Files
piratepoet/lib/Segments/DynamicSegments/SegmentSaveController.php
John Oleksowicz df119d18a0 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]
2021-12-22 09:36:56 +01:00

41 lines
1.1 KiB
PHP

<?php
namespace MailPoet\Segments\DynamicSegments;
use MailPoet\ConflictException;
use MailPoet\Entities\SegmentEntity;
use MailPoet\NotFoundException;
use MailPoet\Segments\SegmentsRepository;
use MailPoetVendor\Doctrine\ORM\ORMException;
class SegmentSaveController {
/** @var SegmentsRepository */
private $segmentsRepository;
/** @var FilterDataMapper */
private $filterDataMapper;
public function __construct(
SegmentsRepository $segmentsRepository,
FilterDataMapper $filterDataMapper
) {
$this->segmentsRepository = $segmentsRepository;
$this->filterDataMapper = $filterDataMapper;
}
/**
* @throws ConflictException
* @throws NotFoundException
* @throws Exceptions\InvalidFilterException
* @throws ORMException
*/
public function save(array $data = []): SegmentEntity {
$id = isset($data['id']) ? (int)$data['id'] : null;
$name = $data['name'] ?? '';
$description = $data['description'] ?? '';
$filtersData = $this->filterDataMapper->map($data);
return $this->segmentsRepository->createOrUpdate($name, $description, SegmentEntity::TYPE_DYNAMIC, $filtersData, $id);
}
}