56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace MailPoet\Segments;
|
|
|
|
use MailPoet\Doctrine\Repository;
|
|
use MailPoet\Entities\SegmentEntity;
|
|
use MailPoet\NotFoundException;
|
|
|
|
/**
|
|
* @extends Repository<SegmentEntity>
|
|
*/
|
|
class SegmentsRepository extends Repository {
|
|
protected function getEntityClassName() {
|
|
return SegmentEntity::class;
|
|
}
|
|
|
|
public function getWPUsersSegment() {
|
|
return $this->findOneBy(['type' => SegmentEntity::TYPE_WP_USERS]);
|
|
}
|
|
|
|
public function getCountsPerType(): array {
|
|
$results = $this->doctrineRepository->createQueryBuilder('s')
|
|
->select('s.type, COUNT(s) as cnt')
|
|
->where('s.deletedAt IS NULL')
|
|
->groupBy('s.type')
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
$countMap = [];
|
|
foreach ($results as $result) {
|
|
$countMap[$result['type']] = (int)$result['cnt'];
|
|
}
|
|
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;
|
|
}
|
|
}
|