1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file was created by developers working at BitBag |
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\Context; |
12
|
|
|
|
13
|
|
|
use BitBag\SyliusElasticsearchPlugin\Exception\TaxonNotFoundException; |
14
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
15
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
16
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
18
|
|
|
|
19
|
|
|
final class TaxonContext implements TaxonContextInterface |
20
|
|
|
{ |
21
|
|
|
/** @var RequestStack */ |
22
|
|
|
private $requestStack; |
23
|
|
|
|
24
|
|
|
/** @var TaxonRepositoryInterface */ |
25
|
|
|
private $taxonRepository; |
26
|
|
|
|
27
|
|
|
/** @var LocaleContextInterface */ |
28
|
|
|
private $localeContext; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
RequestStack $requestStack, |
32
|
|
|
TaxonRepositoryInterface $taxonRepository, |
33
|
|
|
LocaleContextInterface $localeContext |
34
|
|
|
) { |
35
|
|
|
$this->requestStack = $requestStack; |
36
|
|
|
$this->taxonRepository = $taxonRepository; |
37
|
|
|
$this->localeContext = $localeContext; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getTaxon(): TaxonInterface |
41
|
|
|
{ |
42
|
|
|
$slug = $this->requestStack->getCurrentRequest()->get('slug'); |
43
|
|
|
$localeCode = $this->localeContext->getLocaleCode(); |
44
|
|
|
/** @var TaxonInterface $taxon */ |
45
|
|
|
$taxon = $this->taxonRepository->findOneBySlug($slug, $localeCode); |
|
|
|
|
46
|
|
|
|
47
|
|
|
if (null === $slug || null === $taxon) { |
48
|
|
|
throw new TaxonNotFoundException(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $taxon; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|