<?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\SchoolList;
use Products\NotificationsBundle\Service\Query\NotificationsConditionQueryBuilder;
/**
*
*/
final class SchoolListBuilder 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 SchoolList
*/
public function getList(): AbstractList
{
$list = parent::getList();
if ( ! $list instanceof SchoolList) {
throw new \LogicException();
}
return $list;
}
/**
* {@inheritDoc}
*/
public static function supports($list): bool
{
return ($list === SchoolList::class || $list instanceof SchoolList);
}
/**
* {@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 SchoolList) {
throw new \Exception();
}
return [
'types' => $list->getTypes(),
'school' => $list->getOneRosterId(),
];
}
/**
* {@inheritDoc}
* @return SchoolList
*/
public function unserialize(array $serialized): SchoolList
{
return (new SchoolList())
->setTypes($serialized['types'])
->setOneRosterId($serialized['school']);
}
}