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();
|
||||
if ($segment instanceof SegmentEntity) {
|
||||
$duplicate = $this->segmentSavecontroller->duplicate($segment);
|
||||
return $this->successResponse(
|
||||
$duplicate->asArray(),
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
@ -61,6 +61,11 @@ class SegmentEntity {
|
||||
$this->dynamicFilters = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function __clone() {
|
||||
// reset ID
|
||||
$this->id = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
@ -4,15 +4,23 @@ namespace MailPoet\Segments;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
use MailPoet\Entities\SubscriberSegmentEntity;
|
||||
use MailPoet\Subscribers\SubscriberSegmentRepository;
|
||||
|
||||
class SegmentSaveController {
|
||||
/** @var SegmentsRepository */
|
||||
private $segmentsRepository;
|
||||
|
||||
/** @var SubscriberSegmentRepository */
|
||||
private $subscriberSegmentRepository;
|
||||
|
||||
public function __construct(
|
||||
SegmentsRepository $segmentsRepository
|
||||
SegmentsRepository $segmentsRepository,
|
||||
SubscriberSegmentRepository $subscriberSegmentRepository
|
||||
) {
|
||||
$this->segmentsRepository = $segmentsRepository;
|
||||
$this->subscriberSegmentRepository = $subscriberSegmentRepository;
|
||||
}
|
||||
|
||||
public function save(array $data = []): SegmentEntity {
|
||||
@ -25,6 +33,33 @@ class SegmentSaveController {
|
||||
return $this->segmentsRepository->createOrUpdate($name, $description, $id);
|
||||
}
|
||||
|
||||
public function duplicate(SegmentEntity $segmentEntity): SegmentEntity {
|
||||
$duplicate = clone $segmentEntity;
|
||||
$duplicate->setName(sprintf(__('Copy of %s', 'mailpoet'), $segmentEntity->getName()));
|
||||
|
||||
$this->checkSegmenUniqueName($duplicate->getName(), $duplicate->getId());
|
||||
|
||||
$this->segmentsRepository->persist($duplicate);
|
||||
$this->segmentsRepository->flush();
|
||||
|
||||
$subscriberSegments = $this->subscriberSegmentRepository->findBy(['segment' => $segmentEntity]);
|
||||
foreach ($subscriberSegments as $subscriberSegment) {
|
||||
$subscriber = $subscriberSegment->getSubscriber();
|
||||
if (!$subscriber) {
|
||||
continue;
|
||||
}
|
||||
$subscriberDuplicate = new SubscriberSegmentEntity(
|
||||
$duplicate,
|
||||
$subscriber,
|
||||
SubscriberEntity::STATUS_SUBSCRIBED
|
||||
);
|
||||
$this->subscriberSegmentRepository->persist($subscriberDuplicate);
|
||||
}
|
||||
$this->subscriberSegmentRepository->flush();
|
||||
|
||||
return $duplicate;
|
||||
}
|
||||
|
||||
private function checkSegmenUniqueName(string $name, ?int $id): void {
|
||||
if (!$this->segmentsRepository->isNameUnique($name, $id)) {
|
||||
throw new InvalidArgumentException("Segment with name: '{$name}' already exists.");
|
||||
|
Reference in New Issue
Block a user