src/App/Doctrine/Repository/Content/LegacyProxySearch.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Doctrine\Repository\Content;
  3. use App\Model\Searching\AbstractSearch;
  4. use Cms\CoreBundle\Util\DateTimeUtils;
  5. use DateInterval;
  6. use DateTimeInterface;
  7. use Doctrine\ORM\QueryBuilder;
  8. use Platform\SecurityBundle\Entity\Identity\Account;
  9. class LegacyProxySearch extends AbstractSearch
  10. {
  11.     public const FILTERS = [
  12.         self::FILTERS__ALL,
  13.         self::FILTERS__DRAFT,
  14.         self::FILTERS__PUBLISHED,
  15.         self::FILTERS__MY_DRAFT,
  16.         self::FILTERS__MY_PUBLISHED,
  17.     ];
  18.     const FILTERS__DEFAULT self::FILTERS__ALL;
  19.     public const FILTERS__ALL 'all';
  20.     public const FILTERS__DRAFT 'draft';
  21.     public const FILTERS__PUBLISHED 'published';
  22.     public const FILTERS__MY_DRAFT 'my_draft';
  23.     public const FILTERS__MY_PUBLISHED 'my_published';
  24.     const DIRECTIONS = [
  25.         self::SORTS__NAME => 'ASC',
  26.         self::SORTS__TIMESTAMP => 'DESC',
  27.     ];
  28.     const SORTS__DEFAULT self::SORTS__TIMESTAMP;
  29.     const SORTS__NAME 'name';
  30.     const SORTS__TIMESTAMP 'timestamp';
  31.     /**
  32.      * @var string|null
  33.      */
  34.     private ?string $lookup null;
  35.     /**
  36.      * @var bool
  37.      */
  38.     private bool $defaults true;
  39.     /**
  40.      * @var DateTimeInterface|null
  41.      */
  42.     private ?DateTimeInterface $cutoff null;
  43.     /**
  44.      * @var DateTimeInterface|DateInterval|null
  45.      */
  46.     private $window null;
  47.     /**
  48.      * @var bool|null
  49.      */
  50.     private ?bool $visibility null;
  51.     /**
  52.      * @var bool
  53.      */
  54.     private bool $past true;
  55.     /**
  56.      * @var Account|null
  57.      */
  58.     private ?Account $account;
  59.     /**
  60.      * @return string|null
  61.      */
  62.     public function getLookup(): ?string
  63.     {
  64.         return $this->lookup;
  65.     }
  66.     /**
  67.      * @param string|null $lookup
  68.      * @return $this
  69.      */
  70.     public function setLookup(?string $lookup): self
  71.     {
  72.         $this->lookup $lookup ?: null;
  73.         return $this;
  74.     }
  75.     /**
  76.      * @return bool
  77.      */
  78.     public function hasDefaults(): bool
  79.     {
  80.         return $this->defaults;
  81.     }
  82.     /**
  83.      * @param bool $defaults
  84.      * @return $this
  85.      */
  86.     public function setDefaults(bool $defaults): self
  87.     {
  88.         $this->defaults $defaults;
  89.         return $this;
  90.     }
  91.     /**
  92.      * @return DateTimeInterface
  93.      */
  94.     public function getCutoff(): DateTimeInterface
  95.     {
  96.         return $this->cutoff ?: DateTimeUtils::now();
  97.     }
  98.     /**
  99.      * @param DateTimeInterface|null $cutoff
  100.      * @return $this
  101.      */
  102.     public function setCutoff(?DateTimeInterface $cutoff): self
  103.     {
  104.         $this->cutoff $cutoff;
  105.         return $this;
  106.     }
  107.     /**
  108.      * @return DateTimeInterface|DateInterval|null
  109.      */
  110.     public function getWindow()
  111.     {
  112.         return $this->window;
  113.     }
  114.     /**
  115.      * @param DateTimeInterface|DateInterval|string|null $window
  116.      * @return $this
  117.      */
  118.     public function setWindow($window): self
  119.     {
  120.         switch (true) {
  121.             case empty($window):
  122.             case $window instanceof DateTimeInterface:
  123.             case $window instanceof DateInterval:
  124.                 break;
  125.             case is_string($window):
  126.                 $window = new DateInterval($window);
  127.                 break;
  128.             default:
  129.                 throw new \LogicException();
  130.         }
  131.         $this->window $window ?: null;
  132.         return $this;
  133.     }
  134.     /**
  135.      * @return bool|null
  136.      */
  137.     public function getVisibility(): ?bool
  138.     {
  139.         return $this->visibility;
  140.     }
  141.     /**
  142.      * @param bool|null $visibility
  143.      * @return $this
  144.      */
  145.     public function setVisibility(?bool $visibility): self
  146.     {
  147.         $this->visibility $visibility;
  148.         return $this;
  149.     }
  150.     /**
  151.      * @return bool
  152.      */
  153.     public function isPast(): bool
  154.     {
  155.         return $this->past;
  156.     }
  157.     /**
  158.      * @param bool $past
  159.      * @return $this
  160.      */
  161.     public function setPast(bool $past): self
  162.     {
  163.         $this->past $past;
  164.         return $this;
  165.     }
  166.     /**
  167.      * @return Account|null
  168.      */
  169.     public function getAccount(): ?Account
  170.     {
  171.         return $this->account;
  172.     }
  173.     /**
  174.      * @param Account|null $account
  175.      * @return $this
  176.      */
  177.     public function setAccount(?Account $account): self
  178.     {
  179.         $this->account $account;
  180.         return $this;
  181.     }
  182.     /**
  183.      * @return bool
  184.      */
  185.     public function isFuture(): bool
  186.     {
  187.         return !$this->isPast();
  188.     }
  189.     /**
  190.      * @param bool $future
  191.      * @return $this
  192.      */
  193.     public function setFuture(bool $future): self
  194.     {
  195.         return $this->setPast(!$future);
  196.     }
  197.     /**
  198.      * @param QueryBuilder $qb
  199.      * @return QueryBuilder
  200.      */
  201.     public function filterCutoff(QueryBuilder $qb): QueryBuilder
  202.     {
  203.         return $qb
  204.             ->andWhere(
  205.                 $this->isPast()
  206.                     ? $qb->expr()->lte('objects.createdAt'':cutoff')
  207.                     : $qb->expr()->gte('objects.createdAt'':cutoff')
  208.             )
  209.             ->setParameter(
  210.                 'cutoff',
  211.                 $this->getCutoff() ?: DateTimeUtils::now()
  212.             );
  213.     }
  214.     /**
  215.      * @param QueryBuilder $qb
  216.      * @return QueryBuilder
  217.      */
  218.     public function filterWindow(QueryBuilder $qb): QueryBuilder
  219.     {
  220.         if ($this->getWindow()) {
  221.             $qb
  222.                 ->andWhere(
  223.                     $this->isPast()
  224.                         ? $qb->expr()->gte('objects.createdAt'':window')
  225.                         : $qb->expr()->lte('objects.createdAt'':window')
  226.                 )
  227.                 ->setParameter(
  228.                     'window',
  229.                     ($this->getWindow() instanceof DateInterval)
  230.                         ? ($this->isPast() ? ((clone $this->getCutoff()) - $this->getWindow()) : (clone $this->getCutoff()) + $this->getWindow())
  231.                         : $this->getWindow()
  232.                 )
  233.             ;
  234.         }
  235.         return $qb;
  236.     }
  237.     /**
  238.      * @param QueryBuilder $qb
  239.      * @return QueryBuilder
  240.      */
  241.     public function orderCutoff(QueryBuilder $qb): QueryBuilder
  242.     {
  243.         return $qb->addOrderBy('objects.createdAt'$this->isPast() ? 'DESC' 'ASC');
  244.     }
  245. }