Move method for create or update Segment to repository
[MAILPOET-3167]
This commit is contained in:
@ -4,6 +4,7 @@ namespace MailPoet\Segments;
|
||||
|
||||
use MailPoet\Doctrine\Repository;
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\NotFoundException;
|
||||
|
||||
/**
|
||||
* @extends Repository<SegmentEntity>
|
||||
@ -31,4 +32,24 @@ class SegmentsRepository extends Repository {
|
||||
}
|
||||
return $countMap;
|
||||
}
|
||||
|
||||
public function createOrUpdate(
|
||||
string $name,
|
||||
string $description = '',
|
||||
?int $id = null
|
||||
): SegmentEntity {
|
||||
if ($id) {
|
||||
$segment = $this->findOneById($id);
|
||||
if (!$segment instanceof SegmentEntity) {
|
||||
throw new NotFoundException("Segment with ID [{$id}] was not found.");
|
||||
}
|
||||
$segment->setName($name);
|
||||
$segment->setDescription($description);
|
||||
} else {
|
||||
$segment = new SegmentEntity($name, SegmentEntity::TYPE_DEFAULT, $description);
|
||||
$this->persist($segment);
|
||||
}
|
||||
$this->flush();
|
||||
return $segment;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user