vendor/bitbag/elasticsearch-plugin/src/Context/TaxonContext.php line 50

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * another great project.
  6.  * You can find more information about us on https://bitbag.shop and write us
  7.  * an email on mikolaj.krol@bitbag.pl.
  8.  */
  9. declare(strict_types=1);
  10. namespace BitBag\SyliusElasticsearchPlugin\Context;
  11. use BitBag\SyliusElasticsearchPlugin\Exception\TaxonNotFoundException;
  12. use Sylius\Component\Core\Model\TaxonInterface;
  13. use Sylius\Component\Locale\Context\LocaleContextInterface;
  14. use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. final class TaxonContext implements TaxonContextInterface
  17. {
  18.     /** @var RequestStack */
  19.     private $requestStack;
  20.     /** @var TaxonRepositoryInterface */
  21.     private $taxonRepository;
  22.     /** @var LocaleContextInterface */
  23.     private $localeContext;
  24.     public function __construct(
  25.         RequestStack $requestStack,
  26.         TaxonRepositoryInterface $taxonRepository,
  27.         LocaleContextInterface $localeContext
  28.     ) {
  29.         $this->requestStack $requestStack;
  30.         $this->taxonRepository $taxonRepository;
  31.         $this->localeContext $localeContext;
  32.     }
  33.     public function getTaxon(): TaxonInterface
  34.     {
  35.         $slug $this->requestStack->getCurrentRequest()->get('slug');
  36.         $localeCode $this->localeContext->getLocaleCode();
  37.         /** @var TaxonInterface $taxon */
  38.         $taxon $this->taxonRepository->findOneBySlug($slug$localeCode);
  39.         if (null === $slug || null === $taxon) {
  40.             throw new TaxonNotFoundException();
  41.         }
  42.         return $taxon;
  43.     }
  44. }