<?php
namespace Products\NotificationsBundle\Util\ListBuilder;
use Doctrine\ORM\QueryBuilder;
use Cms\CoreBundle\Util\Doctrine\EntityManager;
use Products\NotificationsBundle\Entity\AbstractList;
use Products\NotificationsBundle\Entity\Lists\DistrictList;
use Products\NotificationsBundle\Service\Query\NotificationsConditionQueryBuilder;
/**
*
*/
final class DistrictListBuilder extends AbstractListBuilder
{
private NotificationsConditionQueryBuilder $notificationsConditionQueryBuilder;
/**
* @param EntityManager $em
* @param NotificationsConditionQueryBuilder $notificationsConditionQueryBuilder
* @param AbstractList|null $list
*/
public function __construct(
EntityManager $em,
NotificationsConditionQueryBuilder $notificationsConditionQueryBuilder,
?AbstractList $list
)
{
parent::__construct($em, $list);
$this->notificationsConditionQueryBuilder = $notificationsConditionQueryBuilder;
}
/**
* {@inheritDoc}
* @return DistrictList
*/
public function getList(): AbstractList
{
$list = parent::getList();
if ( ! $list instanceof DistrictList) {
throw new \LogicException();
}
return $list;
}
/**
* {@inheritDoc}
*/
public static function supports($list): bool
{
return ($list === DistrictList::class || $list instanceof DistrictList);
}
/**
* {@inheritDoc}
*/
public function build(): QueryBuilder
{
return $this->notificationsConditionQueryBuilder->build(
$this->getList()->getConditionQuery()
->setConditionConfig($this->getConfig())
->setConditionContext($this->getList()),
$this->identify(),
);
}
/**
* {@inheritDoc}
*/
public function serialize(?AbstractList $list = null): array
{
$list = $list ?: $this->getList();
if ( ! $list instanceof DistrictList) {
throw new \Exception();
}
return [
'types' => $list->getTypes(),
'district' => $list->getOneRosterId(),
];
}
/**
* {@inheritDoc}
* @return DistrictList
*/
public function unserialize(array $serialized): DistrictList
{
return (new DistrictList())
->setTypes($serialized['types'])
->setOneRosterId($serialized['district']);
}
}