Refactor dynamic segments trash and restore
[MAILPOET-3177]
This commit is contained in:
committed by
Veljko V
parent
de9b3689bd
commit
e1eea57d32
@ -120,49 +120,45 @@ class DynamicSegments extends APIEndpoint {
|
||||
}
|
||||
|
||||
public function trash($data = []) {
|
||||
if (isset($data['id'])) {
|
||||
$id = (int)$data['id'];
|
||||
} else {
|
||||
if (!isset($data['id'])) {
|
||||
return $this->errorResponse([
|
||||
Error::BAD_REQUEST => WPFunctions::get()->__('Missing mandatory argument `id`.', 'mailpoet'),
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$segment = $this->dynamicSegmentsLoader->load($id);
|
||||
$segment->trash();
|
||||
return $this->successResponse(
|
||||
$segment->asArray(),
|
||||
['count' => 1]
|
||||
);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$segment = $this->getSegment($data);
|
||||
if ($segment === null) {
|
||||
return $this->errorResponse([
|
||||
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 = []) {
|
||||
if (isset($data['id'])) {
|
||||
$id = (int)$data['id'];
|
||||
} else {
|
||||
if (!isset($data['id'])) {
|
||||
return $this->errorResponse([
|
||||
Error::BAD_REQUEST => WPFunctions::get()->__('Missing mandatory argument `id`.', 'mailpoet'),
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$segment = $this->dynamicSegmentsLoader->load($id);
|
||||
$segment->restore();
|
||||
return $this->successResponse(
|
||||
$segment->asArray(),
|
||||
['count' => 1]
|
||||
);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$segment = $this->getSegment($data);
|
||||
if ($segment === null) {
|
||||
return $this->errorResponse([
|
||||
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 = []) {
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user