Move method for create or update Segment to repository

[MAILPOET-3167]
This commit is contained in:
Jan Lysý
2020-12-03 15:01:52 +01:00
committed by Veljko V
parent 37cef4fb34
commit 79abec8f13
2 changed files with 26 additions and 33 deletions

View File

@ -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;
}
}