Passed
Push — master ( 7a9467...d690b4 )
by Mikołaj
03:48
created

TaxonContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getTaxon() 0 11 3
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $taxon returns the type Sylius\Component\Taxonomy\Model\TaxonInterface which includes types incompatible with the type-hinted return Sylius\Component\Core\Model\TaxonInterface.
Loading history...
68
    }
69
}
70