Failed Conditions
Pull Request — master (#179)
by Łukasz
02:52
created

ProductDetailsQuery::findOneBySlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sylius\ShopApiPlugin\Query;
6
7
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
8
use Sylius\Component\Core\Model\ChannelInterface;
9
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
10
use Sylius\Component\Locale\Model\Locale;
11
use Sylius\ShopApiPlugin\Factory\ProductViewFactoryInterface;
12
use Sylius\ShopApiPlugin\View\ProductView;
13
use Webmozart\Assert\Assert;
14
15
final class ProductDetailsQuery implements ProductDetailsQueryInterface
16
{
17
    /** @var ChannelRepositoryInterface */
18
    private $channelRepository;
19
20
    /** @var ProductRepositoryInterface */
21
    private $productRepository;
22
23
    /** @var ProductViewFactoryInterface */
24
    private $productViewFactory;
25
26
    public function __construct(
27
        ChannelRepositoryInterface $channelRepository,
28
        ProductRepositoryInterface $productRepository,
29
        ProductViewFactoryInterface $productViewFactory
30
    ) {
31
        $this->channelRepository = $channelRepository;
32
        $this->productRepository = $productRepository;
33
        $this->productViewFactory = $productViewFactory;
34
    }
35
36
    public function findOneBySlug(string $channelCode, string $productSlug, ?string $localeCode): ProductView
37
    {
38
        $channel = $this->getChannel($channelCode);
39
        $localeCode = $this->getLocaleCode($localeCode, $channel);
40
41
        $product = $this->productRepository->findOneByChannelAndSlug($channel, $localeCode, $productSlug);
42
43
        Assert::notNull($product, sprintf('Product with slug %s has not been found in %s locale.', $productSlug, $localeCode));
44
45
        return $this->productViewFactory->create($product, $channel, $localeCode);
0 ignored issues
show
Bug introduced by
It seems like $product defined by $this->productRepository...caleCode, $productSlug) on line 41 can be null; however, Sylius\ShopApiPlugin\Fac...toryInterface::create() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
46
    }
47
48
    public function findOneByCode(string $channelCode, string $productCode, ?string $localeCode): ProductView
49
    {
50
        $channel = $this->getChannel($channelCode);
51
        $localeCode = $this->getLocaleCode($localeCode, $channel);
52
53
        $product = $this->productRepository->findOneByCode($productCode);
54
55
        Assert::notNull($product, sprintf('Product with code %s has not been found.', $productCode));
56
        Assert::true($product->hasChannel($channel), sprintf('Channel with code %s has not been found.', $channelCode));
57
58
        return $this->productViewFactory->create($product, $channel, $localeCode);
0 ignored issues
show
Bug introduced by
It seems like $product defined by $this->productRepository...OneByCode($productCode) on line 53 can be null; however, Sylius\ShopApiPlugin\Fac...toryInterface::create() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
59
    }
60
61
    /**
62
     * @param string $localeCode
63
     * @param iterable|Locale[] $supportedLocales
64
     */
65
    private function assertLocaleSupport(string $localeCode, iterable $supportedLocales)
66
    {
67
        $supportedLocaleCodes = [];
68
        foreach ($supportedLocales as $locale) {
69
            $supportedLocaleCodes[] = $locale->getCode();
70
        }
71
72
        Assert::oneOf($localeCode, $supportedLocaleCodes);
73
    }
74
75
    private function getChannel(?string $channelCode): ChannelInterface
76
    {
77
        /** @var ChannelInterface $channel */
78
        $channel = $this->channelRepository->findOneByCode($channelCode);
79
80
        Assert::notNull($channel, sprintf('Channel with code %s has not been found.', $channelCode));
81
82
        return $channel;
83
    }
84
85
    private function getLocaleCode(?string $localeCode, ChannelInterface $channel): string
86
    {
87
        $localeCode = $localeCode ?? $channel->getDefaultLocale()->getCode();
88
        $this->assertLocaleSupport($localeCode, $channel->getLocales());
0 ignored issues
show
Documentation introduced by
$channel->getLocales() is of type object<Doctrine\Common\C...Model\LocaleInterface>>, but the function expects a object<Sylius\ShopApiPlu...t\Locale\Model\Locale>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
90
        return $localeCode;
91
    }
92
}
93