|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the CleverAge/EAVProcessBundle package. |
| 4 | + * |
| 5 | + * Copyright (c) 2015-2019 Clever-Age |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace CleverAge\EAVProcessBundle\Task; |
| 12 | + |
| 13 | +use CleverAge\EAVManager\EAVModelBundle\Entity\DataRepository; |
| 14 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 15 | +use Doctrine\ORM\EntityManagerInterface; |
| 16 | +use Doctrine\ORM\QueryBuilder; |
| 17 | +use Doctrine\ORM\Tools\Pagination\Paginator; |
| 18 | +use Sidus\EAVModelBundle\Doctrine\EAVFinder; |
| 19 | +use Sidus\EAVModelBundle\Exception\MissingAttributeException; |
| 20 | +use Sidus\EAVModelBundle\Model\FamilyInterface; |
| 21 | +use Sidus\EAVModelBundle\Registry\FamilyRegistry; |
| 22 | +use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; |
| 23 | +use Symfony\Component\OptionsResolver\Options; |
| 24 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 25 | + |
| 26 | +/** |
| 27 | + * Handles EAV Data pagination |
| 28 | + */ |
| 29 | +abstract class AbstractEAVQueryTask extends AbstractEAVTask |
| 30 | +{ |
| 31 | + /** @var EAVFinder */ |
| 32 | + protected $eavFinder; |
| 33 | + |
| 34 | + /** |
| 35 | + * @param EntityManagerInterface $entityManager |
| 36 | + * @param FamilyRegistry $familyRegistry |
| 37 | + * @param EAVFinder $eavFinder |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + EntityManagerInterface $entityManager, |
| 41 | + FamilyRegistry $familyRegistry, |
| 42 | + EAVFinder $eavFinder |
| 43 | + ) { |
| 44 | + parent::__construct($entityManager, $familyRegistry); |
| 45 | + $this->eavFinder = $eavFinder; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritDoc} |
| 50 | + */ |
| 51 | + protected function configureOptions(OptionsResolver $resolver): void |
| 52 | + { |
| 53 | + parent::configureOptions($resolver); |
| 54 | + |
| 55 | + $resolver->setDefaults( |
| 56 | + [ |
| 57 | + 'criteria' => [], |
| 58 | + 'extended_criteria' => [], |
| 59 | + 'repository' => null, |
| 60 | + 'order_by' => [], |
| 61 | + 'limit' => null, |
| 62 | + 'offset' => null, |
| 63 | + ] |
| 64 | + ); |
| 65 | + $resolver->setNormalizer( |
| 66 | + 'repository', |
| 67 | + function (Options $options, $value) { |
| 68 | + if ($value instanceof DataRepository) { |
| 69 | + return $value; |
| 70 | + } |
| 71 | + /** @var FamilyInterface $family */ |
| 72 | + $family = $options['family']; |
| 73 | + |
| 74 | + return $this->entityManager->getRepository($family->getDataClass()); |
| 75 | + } |
| 76 | + ); |
| 77 | + |
| 78 | + $resolver->setAllowedTypes('criteria', ['array']); |
| 79 | + $resolver->setAllowedTypes('extended_criteria', ['array']); |
| 80 | + $resolver->setAllowedTypes('repository', ['NULL', DataRepository::class]); |
| 81 | + $resolver->setAllowedTypes('order_by', ['array']); |
| 82 | + $resolver->setAllowedTypes('limit', ['NULL', 'integer']); |
| 83 | + $resolver->setAllowedTypes('offset', ['NULL', 'integer']); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @deprecated Use getPaginator instead because this method can't handle limit and offset properly |
| 88 | + * |
| 89 | + * @param ProcessState $state |
| 90 | + * @param string $alias |
| 91 | + * |
| 92 | + * @throws \InvalidArgumentException |
| 93 | + * @throws \UnexpectedValueException |
| 94 | + * @throws \LogicException |
| 95 | + * @throws MissingAttributeException |
| 96 | + * @throws ExceptionInterface |
| 97 | + * |
| 98 | + * @return QueryBuilder |
| 99 | + */ |
| 100 | + protected function getQueryBuilder(ProcessState $state, $alias = 'e'): QueryBuilder |
| 101 | + { |
| 102 | + $options = $this->getOptions($state); |
| 103 | + |
| 104 | + $criteria = $options['extended_criteria']; |
| 105 | + foreach ($options['criteria'] as $key => $value) { |
| 106 | + $criteria[] = [ |
| 107 | + $key, |
| 108 | + \is_array($value) ? 'in' : '=', |
| 109 | + $value, |
| 110 | + ]; |
| 111 | + } |
| 112 | + $qb = $this->eavFinder->getFilterByQb($options['family'], $criteria, $options['order_by'], $alias); |
| 113 | + |
| 114 | + $qb->distinct(); |
| 115 | + |
| 116 | + return $qb; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * If a limit or an offset is specified, we are forced to use a paginator to handle joins properly |
| 121 | + * |
| 122 | + * @param ProcessState $state |
| 123 | + * @param string $alias |
| 124 | + * |
| 125 | + * @throws \InvalidArgumentException |
| 126 | + * @throws \UnexpectedValueException |
| 127 | + * @throws MissingAttributeException |
| 128 | + * @throws \LogicException |
| 129 | + * @throws ExceptionInterface |
| 130 | + * |
| 131 | + * @return Paginator |
| 132 | + */ |
| 133 | + protected function getPaginator(ProcessState $state, $alias = 'e'): Paginator |
| 134 | + { |
| 135 | + $options = $this->getOptions($state); |
| 136 | + /** @noinspection PhpDeprecationInspection */ |
| 137 | + $paginator = new Paginator($this->getQueryBuilder($state, $alias)); |
| 138 | + if (null !== $options['limit']) { |
| 139 | + $paginator->getQuery()->setMaxResults($options['limit']); |
| 140 | + } |
| 141 | + if (null !== $options['offset']) { |
| 142 | + $paginator->getQuery()->setFirstResult($options['offset']); |
| 143 | + } |
| 144 | + |
| 145 | + return $paginator; |
| 146 | + } |
| 147 | +} |
0 commit comments