Completed
Pull Request — master (#73)
by Mikołaj
16:15
created

RenderBlockExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
c 1
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 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\CmsPlugin\Twig\Extension;
14
15
use BitBag\CmsPlugin\Entity\BlockInterface;
16
use BitBag\CmsPlugin\Exception\TemplateTypeNotFound;
17
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface;
18
use Psr\Log\LoggerInterface;
19
20
/**
21
 * @author Patryk Drapik <[email protected]>
22
 * @author Mikołaj Król <[email protected]>
23
 */
24
final class RenderBlockExtension extends \Twig_Extension
25
{
26
    const TEXT_BLOCK_TEMPLATE = '@BitBagCmsPlugin/Shop/Block/textBlock.html.twig';
27
    const HTML_BLOCK_TEMPLATE = '@BitBagCmsPlugin/Shop/Block/htmlBlock.html.twig';
28
    const IMAGE_BLOCK_TEMPLATE = '@BitBagCmsPlugin/Shop/Block/imageBlock.html.twig';
29
30
    /**
31
     * @var BlockRepositoryInterface
32
     */
33
    private $blockRepository;
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    /**
41
     * @param BlockRepositoryInterface $blockRepository
42
     * @param LoggerInterface $logger
43
     */
44
    public function __construct(
45
        BlockRepositoryInterface $blockRepository,
46
        LoggerInterface $logger
47
    )
48
    {
49
        $this->blockRepository = $blockRepository;
50
        $this->logger = $logger;
51
    }
52
53
    /**
54
     * @return \Twig_SimpleFunction[]
55
     */
56
    public function getFunctions(): array
57
    {
58
        return [
59
            new \Twig_SimpleFunction('bitbag_render_block', [$this, 'renderBlock'], ['needs_environment' => true, 'is_safe' => ['html'],]),
60
        ];
61
    }
62
63
    /**
64
     * @param \Twig_Environment $twigEnvironment
65
     * @param string $code
66
     *
67
     * @return null|string
68
     * @throws TemplateTypeNotFound
69
     */
70
    public function renderBlock(\Twig_Environment $twigEnvironment, $code): ?string
71
    {
72
        $block = $this->blockRepository->findEnabledByCode($code);
73
74
        if (false === $block instanceof BlockInterface) {
75
            $this->logger->warning(sprintf(
76
                'Block with "%s" code was not found in the database.',
77
                $code
78
            ));
79
80
            return null;
81
        }
82
83
        if (BlockInterface::TEXT_BLOCK_TYPE === $block->getType()) {
84
            return $twigEnvironment->render(self::TEXT_BLOCK_TEMPLATE, ['block' => $block]);
85
        }
86
87
        if (BlockInterface::HTML_BLOCK_TYPE === $block->getType()) {
88
            return $twigEnvironment->render(self::HTML_BLOCK_TEMPLATE, ['block' => $block]);
89
        }
90
91
        if (BlockInterface::IMAGE_BLOCK_TYPE === $block->getType()) {
92
            return $twigEnvironment->render(self::IMAGE_BLOCK_TEMPLATE, ['block' => $block]);
93
        }
94
95
        throw new TemplateTypeNotFound($block->getType());
96
    }
97
}