Use Doctrine for Segment duplication
[MAILPOET-3170]
This commit is contained in:
@ -185,26 +185,14 @@ class Segments extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function duplicate($data = []) {
|
public function duplicate($data = []) {
|
||||||
$id = (isset($data['id']) ? (int)$data['id'] : false);
|
$segment = $this->getSegment($data);
|
||||||
$segment = Segment::findOne($id);
|
|
||||||
|
|
||||||
if ($segment instanceof Segment) {
|
if ($segment instanceof SegmentEntity) {
|
||||||
$data = [
|
$duplicate = $this->segmentSavecontroller->duplicate($segment);
|
||||||
'name' => sprintf(__('Copy of %s', 'mailpoet'), $segment->name),
|
return $this->successResponse(
|
||||||
];
|
$this->segmentsResponseBuilder->build($duplicate),
|
||||||
$duplicate = $segment->duplicate($data);
|
['count' => 1]
|
||||||
$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]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return $this->errorResponse([
|
return $this->errorResponse([
|
||||||
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet'),
|
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();
|
$this->dynamicFilters = new ArrayCollection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __clone() {
|
||||||
|
// reset ID
|
||||||
|
$this->id = null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
@ -4,15 +4,23 @@ namespace MailPoet\Segments;
|
|||||||
|
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use MailPoet\Entities\SegmentEntity;
|
use MailPoet\Entities\SegmentEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
use MailPoet\Entities\SubscriberSegmentEntity;
|
||||||
|
use MailPoet\Subscribers\SubscriberSegmentRepository;
|
||||||
|
|
||||||
class SegmentSaveController {
|
class SegmentSaveController {
|
||||||
/** @var SegmentsRepository */
|
/** @var SegmentsRepository */
|
||||||
private $segmentsRepository;
|
private $segmentsRepository;
|
||||||
|
|
||||||
|
/** @var SubscriberSegmentRepository */
|
||||||
|
private $subscriberSegmentRepository;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
SegmentsRepository $segmentsRepository
|
SegmentsRepository $segmentsRepository,
|
||||||
|
SubscriberSegmentRepository $subscriberSegmentRepository
|
||||||
) {
|
) {
|
||||||
$this->segmentsRepository = $segmentsRepository;
|
$this->segmentsRepository = $segmentsRepository;
|
||||||
|
$this->subscriberSegmentRepository = $subscriberSegmentRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function save(array $data = []): SegmentEntity {
|
public function save(array $data = []): SegmentEntity {
|
||||||
@ -25,6 +33,33 @@ class SegmentSaveController {
|
|||||||
return $this->segmentsRepository->createOrUpdate($name, $description, $id);
|
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 {
|
private function checkSegmenUniqueName(string $name, ?int $id): void {
|
||||||
if (!$this->segmentsRepository->isNameUnique($name, $id)) {
|
if (!$this->segmentsRepository->isNameUnique($name, $id)) {
|
||||||
throw new InvalidArgumentException("Segment with name: '{$name}' already exists.");
|
throw new InvalidArgumentException("Segment with name: '{$name}' already exists.");
|
||||||
|
Reference in New Issue
Block a user