PageResourceResolver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 14
c 0
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A findOrLog() 0 14 2
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\PageInterface;
14
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
15
use Psr\Log\LoggerInterface;
16
use Sylius\Component\Locale\Context\LocaleContextInterface;
17
18
final class PageResourceResolver implements PageResourceResolverInterface
19
{
20
    /** @var PageRepositoryInterface */
21
    private $pageRepository;
22
23
    /** @var LocaleContextInterface */
24
    private $localeContext;
25
26
    /** @var LoggerInterface */
27
    private $logger;
28
29
    public function __construct(
30
        PageRepositoryInterface $pageRepository,
31
        LocaleContextInterface $localeContext,
32
        LoggerInterface $logger
33
    ) {
34
        $this->pageRepository = $pageRepository;
35
        $this->localeContext = $localeContext;
36
        $this->logger = $logger;
37
    }
38
39
    public function findOrLog(string $code): ?PageInterface
40
    {
41
        $page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
42
43
        if (false === $page instanceof PageInterface) {
44
            $this->logger->warning(sprintf(
45
                'Page with "%s" code was not found in the database.',
46
                $code
47
            ));
48
49
            return null;
50
        }
51
52
        return $page;
53
    }
54
}
55