Refactor dynamic segments trash and restore

[MAILPOET-3177]
This commit is contained in:
Rostislav Wolny
2021-03-04 15:58:26 +01:00
committed by Veljko V
parent de9b3689bd
commit e1eea57d32
2 changed files with 38 additions and 58 deletions

View File

@ -120,49 +120,45 @@ class DynamicSegments extends APIEndpoint {
} }
public function trash($data = []) { public function trash($data = []) {
if (isset($data['id'])) { if (!isset($data['id'])) {
$id = (int)$data['id'];
} else {
return $this->errorResponse([ return $this->errorResponse([
Error::BAD_REQUEST => WPFunctions::get()->__('Missing mandatory argument `id`.', 'mailpoet'), Error::BAD_REQUEST => WPFunctions::get()->__('Missing mandatory argument `id`.', 'mailpoet'),
]); ]);
} }
try { $segment = $this->getSegment($data);
$segment = $this->dynamicSegmentsLoader->load($id); if ($segment === null) {
$segment->trash();
return $this->successResponse(
$segment->asArray(),
['count' => 1]
);
} catch (\InvalidArgumentException $e) {
return $this->errorResponse([ return $this->errorResponse([
Error::NOT_FOUND => WPFunctions::get()->__('This segment does not exist.', 'mailpoet'), Error::NOT_FOUND => WPFunctions::get()->__('This segment does not exist.', 'mailpoet'),
]); ]);
} }
$this->segmentsRepository->bulkTrash([$segment->getId()], SegmentEntity::TYPE_DYNAMIC);
return $this->successResponse(
$this->segmentsResponseBuilder->build($segment),
['count' => 1]
);
} }
public function restore($data = []) { public function restore($data = []) {
if (isset($data['id'])) { if (!isset($data['id'])) {
$id = (int)$data['id'];
} else {
return $this->errorResponse([ return $this->errorResponse([
Error::BAD_REQUEST => WPFunctions::get()->__('Missing mandatory argument `id`.', 'mailpoet'), Error::BAD_REQUEST => WPFunctions::get()->__('Missing mandatory argument `id`.', 'mailpoet'),
]); ]);
} }
try { $segment = $this->getSegment($data);
$segment = $this->dynamicSegmentsLoader->load($id); if ($segment === null) {
$segment->restore();
return $this->successResponse(
$segment->asArray(),
['count' => 1]
);
} catch (\InvalidArgumentException $e) {
return $this->errorResponse([ return $this->errorResponse([
Error::NOT_FOUND => WPFunctions::get()->__('This segment does not exist.', 'mailpoet'), Error::NOT_FOUND => WPFunctions::get()->__('This segment does not exist.', 'mailpoet'),
]); ]);
} }
$this->segmentsRepository->bulkRestore([$segment->getId()], SegmentEntity::TYPE_DYNAMIC);
return $this->successResponse(
$this->segmentsResponseBuilder->build($segment),
['count' => 1]
);
} }
public function delete($data = []) { public function delete($data = []) {
@ -210,4 +206,10 @@ class DynamicSegments extends APIEndpoint {
]); ]);
} }
} }
private function getSegment(array $data): ?SegmentEntity {
return isset($data['id'])
? $this->segmentsRepository->findOneById((int)$data['id'])
: null;
}
} }

View File

@ -107,55 +107,33 @@ class DynamicSegmentsTest extends \MailPoetTest {
} }
public function testItCanTrashASegment() { public function testItCanTrashASegment() {
DynamicSegment::deleteMany(); $dynamicSegment = $this->createDynamicSegmentEntity('Trash test', 'description');
$dynamicSegment = DynamicSegment::createOrUpdate([
'name' => 'Trash test',
'description' => 'description',
]);
$loader = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader', [
'load' => function () use($dynamicSegment) {
return $dynamicSegment;
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController, $loader); $endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController);
$response = $endpoint->trash(['id' => $dynamicSegment->id]); $response = $endpoint->trash(['id' => $dynamicSegment->getId()]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE); expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->data)->equals($dynamicSegment->asArray()); expect($response->data['name'])->equals($dynamicSegment->getName());
expect($response->meta['count'])->equals(1); expect($response->meta['count'])->equals(1);
$dynamicSegment = DynamicSegment::findOne($dynamicSegment->id); $this->entityManager->refresh($dynamicSegment);
assert($dynamicSegment instanceof DynamicSegment); assert($dynamicSegment instanceof SegmentEntity);
expect($dynamicSegment->deletedAt)->notNull(); expect($dynamicSegment->getDeletedAt())->notNull();
$dynamicSegment->delete();
} }
public function testItCanRestoreASegment() { public function testItCanRestoreASegment() {
DynamicSegment::deleteMany(); $dynamicSegment = $this->createDynamicSegmentEntity('Trash test', 'description');
$dynamicSegment = DynamicSegment::createOrUpdate([
'name' => 'Restore test',
'description' => 'description',
]);
$loader = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader', [
'load' => function () use($dynamicSegment) {
return $dynamicSegment;
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController, $loader); $endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController);
$response = $endpoint->restore(['id' => $dynamicSegment->id]); $response = $endpoint->restore(['id' => $dynamicSegment->getId()]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE); expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->data)->equals($dynamicSegment->asArray()); expect($response->data['name'])->equals($dynamicSegment->getName());
expect($response->meta['count'])->equals(1); expect($response->meta['count'])->equals(1);
$dynamicSegment = DynamicSegment::findOne($dynamicSegment->id); $this->entityManager->refresh($dynamicSegment);
assert($dynamicSegment instanceof DynamicSegment); assert($dynamicSegment instanceof SegmentEntity);
expect($dynamicSegment->deletedAt)->equals(null); expect($dynamicSegment->getDeletedAt())->null();
$dynamicSegment->delete();
} }
public function testItCanDeleteASegment() { public function testItCanDeleteASegment() {