Completed
Pull Request — master (#95)
by
unknown
01:26
created

PageResourceResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A findOrLog() 0 15 2
1
<?php
2
3
/**
4
 * This file was created by the 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\SyliusCmsPlugin\Resolver;
14
15
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
16
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
17
use Psr\Log\LoggerInterface;
18
use Sylius\Component\Locale\Context\LocaleContextInterface;
19
20
/**
21
 * @author Mikołaj Król <[email protected]>
22
 */
23
final class PageResourceResolver implements PageResourceResolverInterface
24
{
25
    /**
26
     * @var PageRepositoryInterface
27
     */
28
    private $pageRepository;
29
30
    /**
31
     * @var LocaleContextInterface
32
     */
33
    private $localeContext;
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    /**
41
     * @param PageRepositoryInterface $pageRepository
42
     * @param LocaleContextInterface $localeContext
43
     * @param LoggerInterface $logger
44
     */
45
    public function __construct(
46
        PageRepositoryInterface $pageRepository,
47
        LocaleContextInterface $localeContext,
48
        LoggerInterface $logger
49
    )
50
    {
51
        $this->pageRepository = $pageRepository;
52
        $this->localeContext = $localeContext;
53
        $this->logger = $logger;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function findOrLog(string $code): ?PageInterface
60
    {
61
        $page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
62
63
        if (false === $page instanceof PageInterface) {
64
            $this->logger->warning(sprintf(
65
                'Page with "%s" code was not found in the database.',
66
                $code
67
            ));
68
69
            return null;
70
        }
71
72
        return $page;
73
    }
74
}
75