MediaResourceResolver::findOrLog()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
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\SyliusCmsPlugin\Resolver;
12
13
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
14
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
15
use Psr\Log\LoggerInterface;
16
use Sylius\Component\Channel\Context\ChannelContextInterface;
17
use Sylius\Component\Locale\Context\LocaleContextInterface;
18
use Webmozart\Assert\Assert;
19
20
final class MediaResourceResolver implements MediaResourceResolverInterface
21
{
22
    /** @var MediaRepositoryInterface */
23
    private $mediaRepository;
24
25
    /** @var LocaleContextInterface */
26
    private $localeContext;
27
28
    /** @var ChannelContextInterface */
29
    private $channelContext;
30
31
    /** @var LoggerInterface */
32
    private $logger;
33
34
    public function __construct(
35
        MediaRepositoryInterface $mediaRepository,
36
        LocaleContextInterface $localeContext,
37
        ChannelContextInterface $channelContext,
38
        LoggerInterface $logger
39
    ) {
40
        $this->mediaRepository = $mediaRepository;
41
        $this->localeContext = $localeContext;
42
        $this->channelContext = $channelContext;
43
        $this->logger = $logger;
44
    }
45
46
    public function findOrLog(string $code): ?MediaInterface
47
    {
48
        Assert::notNull($this->channelContext->getChannel()->getCode());
49
        $media = $this->mediaRepository->findOneEnabledByCode(
50
            $code,
51
            $this->localeContext->getLocaleCode(),
52
            $this->channelContext->getChannel()->getCode()
0 ignored issues
show
Bug introduced by
It seems like $this->channelContext->getChannel()->getCode() can also be of type null; however, parameter $channelCode of BitBag\SyliusCmsPlugin\R...:findOneEnabledByCode() 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

52
            /** @scrutinizer ignore-type */ $this->channelContext->getChannel()->getCode()
Loading history...
53
        );
54
55
        if (false === $media instanceof MediaInterface) {
56
            $this->logger->warning(sprintf(
57
                'Media with "%s" code was not found in the database.',
58
                $code
59
            ));
60
61
            return null;
62
        }
63
64
        return $media;
65
    }
66
}
67