src/App/Doctrine/Repository/Feed/FeedSearch.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Doctrine\Repository\Feed;
  3. use App\Entity\Feed\AbstractEntry;
  4. use App\Entity\System\School;
  5. use App\Model\Searching\AbstractSearch;
  6. use Cms\ContainerBundle\Entity\Container;
  7. use Cms\CoreBundle\Util\DateTimeUtils;
  8. use DateTimeInterface;
  9. use Doctrine\ORM\QueryBuilder;
  10. use Platform\SecurityBundle\Entity\Identity\Account;
  11. use Products\NotificationsBundle\Entity\Profile;
  12. class FeedSearch extends AbstractSearch
  13. {
  14.     public const SORTS__DEFAULT 'default';
  15.     public const DIRECTIONS = [
  16.         self::SORTS__DEFAULT => 'ASC',
  17.     ];
  18.     public const STATUS__DRAFT 'draft';
  19.     public const STATUS__SCHEDULED 'scheduled';
  20.     public const STATUS__LIVE 'live';
  21.     public const STATUSES = [
  22.         self::STATUS__DRAFT,
  23.         self::STATUS__SCHEDULED,
  24.         self::STATUS__LIVE,
  25.     ];
  26.     public const AUTHOR__ME 'me';
  27.     public const AUTHORS = [
  28.         self::AUTHOR__ME,
  29.     ];
  30.     /**
  31.      * @var string|null
  32.      */
  33.     protected ?string $lookup null;
  34.     /**
  35.      * @var DateTimeInterface|null
  36.      */
  37.     protected ?DateTimeInterface $cutoff null;
  38.     /**
  39.      * @var int
  40.      */
  41.     protected int $visibility AbstractEntry::VISIBILITIES__PUBLISHED;
  42.     /**
  43.      * @var bool
  44.      */
  45.     protected bool $boosted true;
  46.     /**
  47.      * @var bool
  48.      */
  49.     protected bool $deep false;
  50.     /**
  51.      * @var bool
  52.      */
  53.     protected bool $past true;
  54.     /**
  55.      * @var bool
  56.      */
  57.     protected bool $district true;
  58.     /**
  59.      * @var array<int>
  60.      */
  61.     protected array $schoolTypes = [];
  62.     /**
  63.      * @var array<School>
  64.      */
  65.     protected array $schools = [];
  66.     /**
  67.      * @var array<Container>
  68.      */
  69.     protected array $departments = [];
  70.     /**
  71.      * @var Account|Profile|null
  72.      */
  73.     protected $user null;
  74.     /**
  75.      * @var Container|null
  76.      */
  77.     protected ?Container $root null;
  78.     /**
  79.      * @var int|null
  80.      */
  81.     protected ?int $channels null;
  82.     /**
  83.      * @var string|null
  84.      */
  85.     protected ?string $type null;
  86.     /**
  87.      * @var string|null
  88.      */
  89.     protected ?string $author null;
  90.     /**
  91.      * @var string|null
  92.      */
  93.     protected ?string $status null;
  94.     /**
  95.      * @return string|null
  96.      */
  97.     public function getLookup(): ?string
  98.     {
  99.         return $this->lookup;
  100.     }
  101.     /**
  102.      * @param string|null $lookup
  103.      * @return $this
  104.      */
  105.     public function setLookup(?string $lookup): self
  106.     {
  107.         $this->lookup $lookup ?: null;
  108.         return $this;
  109.     }
  110.     /**
  111.      * @return DateTimeInterface
  112.      */
  113.     public function getCutoff(): DateTimeInterface
  114.     {
  115.         return $this->cutoff ?: DateTimeUtils::now();
  116.     }
  117.     /**
  118.      * @param DateTimeInterface|null $cutoff
  119.      * @return $this
  120.      */
  121.     public function setCutoff(?DateTimeInterface $cutoff): self
  122.     {
  123.         $this->cutoff $cutoff;
  124.         return $this;
  125.     }
  126.     /**
  127.      * @return int
  128.      */
  129.     public function getVisibility(): int
  130.     {
  131.         return $this->visibility;
  132.     }
  133.     /**
  134.      * @param int|null $visibility
  135.      * @return $this
  136.      */
  137.     public function setVisibility(?int $visibility): self
  138.     {
  139.         $this->visibility $visibility ?: AbstractEntry::VISIBILITIES__PUBLISHED;
  140.         return $this;
  141.     }
  142.     /**
  143.      * @return bool
  144.      */
  145.     public function getBoosted(): bool
  146.     {
  147.         return $this->boosted;
  148.     }
  149.     /**
  150.      * @param bool $boosted
  151.      * @return $this
  152.      */
  153.     public function setBoosted(bool $boosted): self
  154.     {
  155.         $this->boosted $boosted;
  156.         return $this;
  157.     }
  158.     /**
  159.      * @return bool
  160.      */
  161.     public function isDeep(): bool
  162.     {
  163.         return $this->deep;
  164.     }
  165.     /**
  166.      * @param bool $deep
  167.      * @return $this
  168.      */
  169.     public function setDeep(bool $deep): self
  170.     {
  171.         $this->deep $deep;
  172.         return $this;
  173.     }
  174.     /**
  175.      * @return bool
  176.      */
  177.     public function isPast(): bool
  178.     {
  179.         return $this->past;
  180.     }
  181.     /**
  182.      * @param bool $past
  183.      * @return $this
  184.      */
  185.     public function setPast(bool $past): self
  186.     {
  187.         $this->past $past;
  188.         return $this;
  189.     }
  190.     /**
  191.      * @return bool
  192.      */
  193.     public function isFuture(): bool
  194.     {
  195.         return ! $this->isPast();
  196.     }
  197.     /**
  198.      * @param bool $future
  199.      * @return $this
  200.      */
  201.     public function setFuture(bool $future): self
  202.     {
  203.         return $this->setPast( ! $future);
  204.     }
  205.     /**
  206.      * @return bool
  207.      */
  208.     public function hasDistrict(): bool
  209.     {
  210.         return $this->district;
  211.     }
  212.     /**
  213.      * @param bool $district
  214.      * @return $this
  215.      */
  216.     public function setDistrict(bool $district): self
  217.     {
  218.         $this->district $district;
  219.         return $this;
  220.     }
  221.     /**
  222.      * @return array
  223.      */
  224.     public function getSchoolTypes(): array
  225.     {
  226.         return $this->schoolTypes;
  227.     }
  228.     /**
  229.      * @param array<int>|int|null $schoolTypes
  230.      * @return $this
  231.      */
  232.     public function setSchoolTypes($schoolTypes): self
  233.     {
  234.         $this->schoolTypes = [];
  235.         if ($schoolTypes) {
  236.             if ( ! is_array($schoolTypes)) {
  237.                 $schoolTypes = [$schoolTypes];
  238.             }
  239.             foreach ($schoolTypes as $schoolType) {
  240.                 $this->addSchoolType($schoolType);
  241.             }
  242.         }
  243.         return $this;
  244.     }
  245.     /**
  246.      * @param int $schoolType
  247.      * @return $this
  248.      */
  249.     public function addSchoolType(int $schoolType): self
  250.     {
  251.         if ( ! in_array($schoolType$this->schoolTypestrue)) {
  252.             $this->schoolTypes[] = $schoolType;
  253.         }
  254.         return $this;
  255.     }
  256.     /**
  257.      * @return array<School>
  258.      */
  259.     public function getSchools(): array
  260.     {
  261.         return $this->schools;
  262.     }
  263.     /**
  264.      * @return School
  265.      */
  266.     public function getSchool(): ?School
  267.     {
  268.         return !empty($this->schools) ? $this->schools[0] : null;
  269.     }
  270.     /**
  271.      * @param School
  272.      * @return $this
  273.      */
  274.     public function setSchool($school): self
  275.     {
  276.        return $this->setSchools($school);
  277.     }
  278.     /**
  279.      * @param School|array<School>|null $schools
  280.      * @return $this
  281.      */
  282.     public function setSchools($schools): self
  283.     {
  284.         $this->schools = [];
  285.         if ($schools) {
  286.             if ( ! is_array($schools)) {
  287.                 $schools = [$schools];
  288.             }
  289.             foreach ($schools as $school) {
  290.                 $this->addSchool($school);
  291.             }
  292.         }
  293.         return $this;
  294.     }
  295.     /**
  296.      * @param School $school
  297.      * @return $this
  298.      */
  299.     public function addSchool(School $school): self
  300.     {
  301.         if ( ! in_array($school$this->schoolstrue)) {
  302.             $this->schools[] = $school;
  303.         }
  304.         return $this;
  305.     }
  306.     /**
  307.      * @return array<Container>
  308.      */
  309.     public function getDepartments(): array
  310.     {
  311.         return $this->departments;
  312.     }
  313.     /**
  314.      * @param Container|array<Container>|null $departments
  315.      * @return $this
  316.      */
  317.     public function setDepartments($departments): self
  318.     {
  319.         $this->departments = [];
  320.         if ($departments) {
  321.             if ( ! is_array($departments)) {
  322.                 $departments = [$departments];
  323.             }
  324.             foreach ($departments as $department) {
  325.                 $this->addDepartment($department);
  326.             }
  327.         }
  328.         return $this;
  329.     }
  330.     /**
  331.      * @param Container $department
  332.      * @return $this
  333.      */
  334.     public function addDepartment(Container $department): self
  335.     {
  336.         if ( ! in_array($department$this->departmentstrue)) {
  337.             $this->departments[] = $department;
  338.         }
  339.         return $this;
  340.     }
  341.     /**
  342.      * @return Profile|Account|null
  343.      */
  344.     public function getUser()
  345.     {
  346.         return $this->user;
  347.     }
  348.     /**
  349.      * @param Account|Profile|null $user
  350.      * @return $this
  351.      */
  352.     public function setUser($user): self
  353.     {
  354.         if ($user && ! $user instanceof Account && ! $user instanceof Profile) {
  355.             throw new \RuntimeException();
  356.         }
  357.         $this->user $user ?: null;
  358.         return $this;
  359.     }
  360.     /**
  361.      * @return Container|null
  362.      */
  363.     public function getRoot(): ?Container
  364.     {
  365.         return $this->root;
  366.     }
  367.     /**
  368.      * @param Container|null $root
  369.      * @return $this
  370.      */
  371.     public function setRoot(?Container $root): self
  372.     {
  373.         $this->root $root;
  374.         return $this;
  375.     }
  376.     /**
  377.      * @param QueryBuilder $qb
  378.      * @return void
  379.      */
  380.     public function filterCutoff(QueryBuilder $qb): void
  381.     {
  382.         $qb
  383.             ->andWhere(
  384.                 $this->isPast()
  385.                     ? $qb->expr()->lte('entries.timestamp'':cutoff')
  386.                     : $qb->expr()->gte('entries.timestamp'':cutoff')
  387.             )
  388.             ->setParameter(
  389.                 'cutoff',
  390.                 $this->getCutoff() ?: DateTimeUtils::now()
  391.             );
  392.     }
  393.     /**
  394.      * @param QueryBuilder $qb
  395.      * @return void
  396.      */
  397.     public function orderCutoff(QueryBuilder $qb): void
  398.     {
  399.         $qb->addOrderBy(
  400.             'entries.timestamp',
  401.             $this->isPast() ? 'DESC' 'ASC',
  402.         );
  403.     }
  404.     /**
  405.      * @return int|null
  406.      */
  407.     public function getChannels(): ?int
  408.     {
  409.         return $this->channels;
  410.     }
  411.     /**
  412.      * @param int|null $channels
  413.      * @return self
  414.      */
  415.     public function setChannels(?int $channels): self
  416.     {
  417.         $this->channels $channels;
  418.         return $this;
  419.     }
  420.     /**
  421.      * @return string|null
  422.      */
  423.     public function getType(): ?string
  424.     {
  425.         return $this->type;
  426.     }
  427.     /**
  428.      * @param string|null $type
  429.      * @return $this
  430.      */
  431.     public function setType(?string $type): self
  432.     {
  433.         $this->type $type;
  434.         return $this;
  435.     }
  436.     /**
  437.      * @return string|null
  438.      */
  439.     public function getStatus(): ?string
  440.     {
  441.         return $this->status;
  442.     }
  443.     /**
  444.      * @param string|null $status
  445.      * @return $this
  446.      */
  447.     public function setStatus(?string $status): self
  448.     {
  449.         $this->status $status;
  450.         return $this;
  451.     }
  452.     /**
  453.      * @return string|null
  454.      */
  455.     public function getAuthor(): ?string
  456.     {
  457.         return $this->author;
  458.     }
  459.     /**
  460.      * @param string|null $author
  461.      * @return $this
  462.      */
  463.     public function setAuthor(?string $author): self
  464.     {
  465.         $this->author $author;
  466.         return $this;
  467.     }
  468.     /**
  469.      * @return array
  470.      */
  471.     public function jsonSerialize(): array
  472.     {
  473.         return array_merge(
  474.             parent::jsonSerialize(),
  475.             [
  476.                 'lookup' => $this->getLookup(),
  477.                 'cutoff' => $this->getCutoff()->format(DateTimeInterface::RFC3339),
  478.                 'visibility' => $this->getVisibility(),
  479.                 'boosted' => $this->getBoosted(),
  480.                 'deep' => $this->isDeep(),
  481.                 'past' => $this->isPast(),
  482.                 'district' => $this->hasDistrict(),
  483.                 'schoolTypes' => $this->getSchoolTypes(),
  484.                 'schools' => array_map(
  485.                     static function (School $school) {
  486.                         return $school->getUlidAsString();
  487.                     },
  488.                     $this->getSchools(),
  489.                 ),
  490.                 'departments' => array_map(
  491.                     static function (Container $department) {
  492.                         return $department->getId();
  493.                     },
  494.                     $this->getDepartments(),
  495.                 ),
  496.                 'user' => $this->getUser() ? $this->getUser()->getId() : null,
  497.                 'root' => $this->getRoot() ? $this->getRoot()->getId() : null,
  498.             ],
  499.         );
  500.     }
  501. }