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

RenderBlockExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getFunctions() 0 6 1
A renderBlock() 0 12 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\Twig\Extension;
14
15
use BitBag\SyliusCmsPlugin\Exception\TemplateTypeNotFound;
16
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
17
use BitBag\SyliusCmsPlugin\Resolver\BlockResourceResolverInterface;
18
use BitBag\SyliusCmsPlugin\Resolver\BlockTemplateResolverInterface;
19
20
/**
21
 * @author Mikołaj Król <[email protected]>
22
 * @author Patryk Drapik <[email protected]>
23
 */
24
final class RenderBlockExtension extends \Twig_Extension
25
{
26
    /**
27
     * @var BlockRepositoryInterface
28
     */
29
    private $blockRepository;
30
31
    /**
32
     * @var BlockTemplateResolverInterface
33
     */
34
    private $blockTemplateResolver;
35
36
    /**
37
     * @var BlockResourceResolverInterface
38
     */
39
    private $blockResourceResolver;
40
41
    /**
42
     * @param BlockRepositoryInterface $blockRepository
43
     * @param BlockTemplateResolverInterface $blockTemplateResolver
44
     * @param BlockResourceResolverInterface $blockResourceResolver
45
     */
46
    public function __construct(
47
        BlockRepositoryInterface $blockRepository,
48
        BlockTemplateResolverInterface $blockTemplateResolver,
49
        BlockResourceResolverInterface $blockResourceResolver
50
    )
51
    {
52
        $this->blockRepository = $blockRepository;
53
        $this->blockTemplateResolver = $blockTemplateResolver;
54
        $this->blockResourceResolver = $blockResourceResolver;
55
    }
56
57
    /**
58
     * @return \Twig_SimpleFunction[]
59
     */
60
    public function getFunctions(): array
61
    {
62
        return [
63
            new \Twig_SimpleFunction('bitbag_sylius_cms_plugin_render_block', [$this, 'renderBlock'], ['needs_environment' => true, 'is_safe' => ['html']]),
64
        ];
65
    }
66
67
    /**
68
     * @param \Twig_Environment $twigEnvironment
69
     * @param string $code
70
     *
71
     * @return string
72
     *
73
     * @throws TemplateTypeNotFound
74
     */
75
    public function renderBlock(\Twig_Environment $twigEnvironment, string $code): string
76
    {
77
        $block = $this->blockResourceResolver->findOrLog($code);
78
79
        if (null !== $block) {
80
            $template = $this->blockTemplateResolver->resolveTemplate($block);
81
82
            return $twigEnvironment->render($template, ['block' => $block]);
83
        }
84
85
        return '';
86
    }
87
}
88