src/Products/NotificationsBundle/Util/ListBuilder/AbstractListBuilder.php line 87

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Util\ListBuilder;
  3. use App\Model\Query\ConditionQuery\ConditionConfig;
  4. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  5. use Doctrine\ORM\QueryBuilder;
  6. use Products\NotificationsBundle\Entity\AbstractList;
  7. use Products\NotificationsBundle\Entity\AbstractRecipient;
  8. use Products\NotificationsBundle\Entity\AutomationRecord;
  9. use Products\NotificationsBundle\Entity\NotificationsConfig;
  10. use Products\NotificationsBundle\Entity\Profile;
  11. use Products\NotificationsBundle\Entity\ProfileContact;
  12. use Products\NotificationsBundle\Entity\ProfileRelationship;
  13. use Products\NotificationsBundle\Entity\Recipients\AppRecipient;
  14. use Products\NotificationsBundle\Entity\Recipients\EmailRecipient;
  15. use Products\NotificationsBundle\Entity\Recipients\PhoneRecipient;
  16. use Products\NotificationsBundle\Entity\Student;
  17. /**
  18.  *
  19.  */
  20. abstract class AbstractListBuilder
  21. {
  22.     public const ENTITY_MAPPING = [
  23.         Profile::class => self::ENTITIES__PROFILES,
  24.         AbstractRecipient::class => self::ENTITIES__RECIPIENTS,
  25.         EmailRecipient::class => self::ENTITIES__RECIPIENTS,
  26.         PhoneRecipient::class => self::ENTITIES__RECIPIENTS,
  27.         AppRecipient::class => self::ENTITIES__RECIPIENTS,
  28.         Student::class => self::ENTITIES__STUDENTS,
  29.         ProfileContact::class => self::ENTITIES__PROFILE_CONTACTS,
  30.         ProfileRelationship::class => self::ENTITIES__PROFILE_RELATIONSHIPS,
  31.         AutomationRecord::class => self::ENTITIES__PROFILE_AUTOMATION_RECORDS,
  32.     ];
  33.     public const ENTITIES__PROFILES 'profiles';
  34.     public const ENTITIES__RECIPIENTS 'contacts';
  35.     public const ENTITIES__STUDENTS 'students';
  36.     public const ENTITIES__PROFILE_CONTACTS 'xrefs';
  37.     public const ENTITIES__PROFILE_RELATIONSHIPS 'relationships';
  38.     public const ENTITIES__PROFILE_AUTOMATION_RECORDS 'automations';
  39.     // DI
  40.     protected EntityManager $em;
  41.     /**
  42.      * The specific list for which this ListBuilder is working with.
  43.      *
  44.      * @var AbstractList|null
  45.      */
  46.     private ?AbstractList $list;
  47.     /**
  48.      * @param EntityManager $em
  49.      * @param AbstractList|null $list
  50.      */
  51.     public function __construct(
  52.         EntityManager $em,
  53.         ?AbstractList $list null
  54.     )
  55.     {
  56.         $this->em $em;
  57.         $this->list $list;
  58.     }
  59.     /**
  60.      * @param AbstractList|string $list
  61.      * @return bool
  62.      */
  63.     abstract public static function supports($list): bool;
  64.     /**
  65.      * @return AbstractList
  66.      */
  67.     public function getList(): AbstractList
  68.     {
  69.         if ( ! $this->list) {
  70.             throw new \Exception();
  71.         }
  72.         return $this->list;
  73.     }
  74.     /**
  75.      * @return ConditionConfig
  76.      */
  77.     public function getConfig(): ConditionConfig
  78.     {
  79.         // TODO: need to find a way to cache this or something; maybe use resolvers???
  80.         $config $this->em->getRepository(NotificationsConfig::class)->findOneBy([
  81.             'tenant' => $this->getList()->getTenant(),
  82.         ]);
  83.         if ( ! $config instanceof NotificationsConfig) {
  84.             return new ConditionConfig();
  85.         }
  86.         return $config->getConfig();
  87.     }
  88.     /**
  89.      * @return QueryBuilder
  90.      */
  91.     abstract public function build(): QueryBuilder;
  92.     /**
  93.      * @param AbstractList|null $list
  94.      * @return array
  95.      */
  96.     abstract public function serialize(?AbstractList $list null): array;
  97.     /**
  98.      * @param array $serialized
  99.      * @return AbstractList
  100.      */
  101.     abstract public function unserialize(array $serialized): AbstractList;
  102.     /**
  103.      * @return QueryBuilder
  104.      */
  105.     public function subquery(): QueryBuilder
  106.     {
  107.         return ($qb $this->build())
  108.             // overwrite the select to just pick the ids
  109.             ->select($qb->getRootAliases()[0].'.id')
  110.             // speed this up by removing the ordering
  111.             ->resetDQLPart('orderBy');
  112.     }
  113.     /**
  114.      * @return string
  115.      */
  116.     protected function identify(): string
  117.     {
  118.         if ($this->list->getId()) {
  119.             return strval($this->list->getId());
  120.         }
  121.         return spl_object_hash($this->list);
  122.     }
  123.     /**
  124.      * @param string $alias
  125.      * @return string
  126.      */
  127.     protected function alias(string $alias): string
  128.     {
  129.         return sprintf(
  130.             '%s%s',
  131.             $alias,
  132.             $this->identify(),
  133.         );
  134.     }
  135.     /**
  136.      * @param string $param
  137.      * @return string
  138.      */
  139.     protected function param(string $param): string
  140.     {
  141.         return sprintf(
  142.             '%s_%s',
  143.             $param,
  144.             $this->identify(),
  145.         );
  146.     }
  147.     /**
  148.      * @param string $alias
  149.      * @param string $prop
  150.      * @return string
  151.      */
  152.     protected function prop(string $aliasstring $prop): string
  153.     {
  154.         return sprintf(
  155.             '%s.%s',
  156.             $this->alias($alias),
  157.             $prop,
  158.         );
  159.     }
  160. }