Completed
Pull Request — master (#81)
by Mikołaj
01:19
created

BlockResourceResolver::findOrLog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
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
14
namespace BitBag\CmsPlugin\Resolver;
15
16
use BitBag\CmsPlugin\Entity\BlockInterface;
17
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface;
18
use Psr\Log\LoggerInterface;
19
20
final class BlockResourceResolver implements BlockResourceResolverInterface
21
{
22
    /**
23
     * @var BlockRepositoryInterface
24
     */
25
    private $blockRepository;
26
27
    /**
28
     * @var LoggerInterface
29
     */
30
    private $logger;
31
32
    /**
33
     * @param BlockRepositoryInterface $blockRepository
34
     * @param LoggerInterface $logger
35
     */
36
    public function __construct(BlockRepositoryInterface $blockRepository, LoggerInterface $logger)
37
    {
38
        $this->blockRepository = $blockRepository;
39
        $this->logger = $logger;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function findOrLog(string $code): ?BlockInterface
46
    {
47
        $block = $this->blockRepository->findEnabledByCode($code);
48
49
        if (false === $block instanceof BlockInterface) {
50
            $this->logger->warning(sprintf(
51
                'Block with "%s" code was not found in the database.',
52
                $code
53
            ));
54
55
            return null;
56
        }
57
58
        return $block;
59
    }
60
}