Passed
Pull Request — master (#360)
by Mateusz
08:07
created

RenderBlockRuntime   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 34
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A renderBlock() 0 11 2
1
<?php
2
3
/*
4
 * This file has been created by 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.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Twig\Runtime;
14
15
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
16
use BitBag\SyliusCmsPlugin\Resolver\BlockResourceResolverInterface;
17
use Twig\Environment;
18
19
final class RenderBlockRuntime implements RenderBlockRuntimeInterface
20
{
21
    /** @var BlockRepositoryInterface */
22
    private $blockRepository;
23
24
    /** @var BlockResourceResolverInterface */
25
    private $blockResourceResolver;
26
27
    /** @var Environment */
28
    private $templatingEngine;
29
30
    private const DEFAULT_TEMPLATE = '@BitBagSyliusCmsPlugin/Shop/Block/show.html.twig';
31
32
    public function __construct(
33
        BlockRepositoryInterface $blockRepository,
34
        BlockResourceResolverInterface $blockResourceResolver,
35
        Environment $templatingEngine
36
    ) {
37
        $this->blockRepository = $blockRepository;
38
        $this->blockResourceResolver = $blockResourceResolver;
39
        $this->templatingEngine = $templatingEngine;
40
    }
41
42
    public function renderBlock(string $code, ?string $template = null): string
43
    {
44
        $block = $this->blockResourceResolver->findOrLog($code);
45
46
        if (null !== $block) {
47
            $template = $template ?? self::DEFAULT_TEMPLATE;
48
49
            return $this->templatingEngine->render($template, ['block' => $block]);
50
        }
51
52
        return '';
53
    }
54
}
55