Use Doctrine for Segment duplication
[MAILPOET-3170]
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user