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