Use Doctrine for Segment duplication

[MAILPOET-3170]
This commit is contained in:
Jan Lysý
2021-01-22 10:51:21 +01:00
committed by Veljko V
parent 180a238b40
commit e08e19902b
3 changed files with 54 additions and 20 deletions

View File

@ -185,26 +185,14 @@ class Segments extends APIEndpoint {
}
public function duplicate($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$segment = Segment::findOne($id);
$segment = $this->getSegment($data);
if ($segment instanceof Segment) {
$data = [
'name' => sprintf(__('Copy of %s', 'mailpoet'), $segment->name),
];
$duplicate = $segment->duplicate($data);
$errors = $duplicate->getErrors();
if (!empty($errors)) {
return $this->errorResponse($errors);
} else {
$duplicate = Segment::findOne($duplicate->id);
if(!$duplicate instanceof Segment) return $this->errorResponse();
return $this->successResponse(
$duplicate->asArray(),
['count' => 1]
);
}
if ($segment instanceof SegmentEntity) {
$duplicate = $this->segmentSavecontroller->duplicate($segment);
return $this->successResponse(
$this->segmentsResponseBuilder->build($duplicate),
['count' => 1]
);
} else {
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet'),
@ -238,4 +226,10 @@ class Segments extends APIEndpoint {
]);
}
}
private function getSegment(array $data): ?SegmentEntity {
return isset($data['id'])
? $this->segmentsRepository->findOneById((int)$data['id'])
: null;
}
}