Issues (36)

src/Context/TaxonContext.php (1 issue)

Labels
Severity
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);
0 ignored issues
show
It seems like $slug can also be of type null; however, parameter $slug of Sylius\Component\Taxonom...erface::findOneBySlug() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $taxon = $this->taxonRepository->findOneBySlug(/** @scrutinizer ignore-type */ $slug, $localeCode);
Loading history...
46
47
        if (null === $slug || null === $taxon) {
48
            throw new TaxonNotFoundException();
49
        }
50
51
        return $taxon;
52
    }
53
}
54