You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 line
1.7KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
  5. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  6. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  7. use Lc\CaracoleBundle\Resolver\SectionResolver;
  8. use Lc\SovBundle\Repository\AbstractRepository as SovAbstractRepository;
  9. trait RepositoryTrait
  10. {
  11. protected $merchantResolver;
  12. protected $sectionResolver;
  13. public function __construct(
  14. EntityManagerInterface $entityManager,
  15. SectionResolver $sectionResolver,
  16. MerchantResolver $merchantResolver
  17. ) {
  18. parent::__construct($entityManager);
  19. $this->merchantResolver = $merchantResolver;
  20. $this->sectionResolver = $sectionResolver;
  21. }
  22. protected function setCriteria(array $criteria) :array
  23. {
  24. $criteria = parent::setCriteria($criteria);
  25. $className = $this->getClassMetadata()->getName();
  26. $entity = new $className;
  27. if ($entity instanceof FilterMerchantInterface) {
  28. if (!isset($criteria['merchant'])) {
  29. $criteria['merchant'] = $this->merchantResolver->getCurrent();
  30. }
  31. if ($criteria['merchant'] === false) {
  32. unset($criteria['merchant']);
  33. }
  34. }
  35. if ($entity instanceof FilterSectionInterface) {
  36. if (!isset($criteria['section'])) {
  37. $criteria['section'] = $this->sectionResolver->getCurrent();
  38. }
  39. if ($criteria['section'] === false) {
  40. unset($criteria['section']);
  41. }
  42. }
  43. return $criteria;
  44. }
  45. }