Refactor duplicate segment name checks

This creates a single method we can rely on to check for the uniqueness
of a segment name and refactors places where we had duplicate code to
use the new method.

[MAILPOET-3998]
This commit is contained in:
John Oleksowicz
2021-12-20 12:24:09 -06:00
committed by Veljko V
parent c23d8cae53
commit df119d18a0
8 changed files with 82 additions and 30 deletions

View File

@@ -2,10 +2,12 @@
namespace MailPoet\Segments;
use InvalidArgumentException;
use MailPoet\ConflictException;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Entities\SubscriberSegmentEntity;
use MailPoet\NotFoundException;
use MailPoetVendor\Doctrine\ORM\EntityManager;
use MailPoetVendor\Doctrine\ORM\ORMException;
class SegmentSaveController {
/** @var SegmentsRepository */
@@ -22,21 +24,27 @@ class SegmentSaveController {
$this->entityManager = $entityManager;
}
/**
* @throws ConflictException
* @throws NotFoundException
* @throws ORMException
*/
public function save(array $data = []): SegmentEntity {
$id = isset($data['id']) ? (int)$data['id'] : null;
$name = $data['name'] ?? '';
$description = $data['description'] ?? '';
$this->checkSegmentUniqueName($name, $id);
return $this->segmentsRepository->createOrUpdate($name, $description, SegmentEntity::TYPE_DEFAULT, [], $id);
}
/**
* @throws ConflictException
*/
public function duplicate(SegmentEntity $segmentEntity): SegmentEntity {
$duplicate = clone $segmentEntity;
$duplicate->setName(sprintf(__('Copy of %s', 'mailpoet'), $segmentEntity->getName()));
$this->checkSegmentUniqueName($duplicate->getName(), $duplicate->getId());
$this->segmentsRepository->verifyNameIsUnique($duplicate->getName(), $duplicate->getId());
$this->entityManager->transactional(function (EntityManager $entityManager) use ($duplicate, $segmentEntity) {
$entityManager->persist($duplicate);
@@ -58,10 +66,4 @@ class SegmentSaveController {
return $duplicate;
}
private function checkSegmentUniqueName(string $name, ?int $id): void {
if (!$this->segmentsRepository->isNameUnique($name, $id)) {
throw new InvalidArgumentException("Segment with name: '{$name}' already exists.");
}
}
}