FrontRender::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Chris\Bundle\FrontRenderBundle\Render;
4
5
use Chris\Bundle\FrontRenderBundle\Event\BeforeRenderEvent;
6
use Chris\Bundle\FrontRenderBundle\Exception\FrontRenderException;
7
use Chris\Bundle\FrontRenderBundle\FrontRenderEvents;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\Templating\EngineInterface;
10
11
class FrontRender
12
{
13
    /**
14
     * @var EngineInterface $twig
15
     */
16
    protected $engine;
17
18
    /**
19
     * @var string $frontPath
20
     */
21
    protected $frontPath;
22
23
    /**
24
     * @var EventDispatcherInterface $dispatcher
25
     */
26
    protected $dispatcher;
27
28
    /**
29
     * @var array $parameters
30
     */
31
    protected $parameters = [];
32
33
    /**
34
     * @param EngineInterface          $engine
35
     * @param EventDispatcherInterface $dispatcher
36
     * @param string                   $frontPath
37
     */
38 8
    public function __construct(EngineInterface $engine, EventDispatcherInterface $dispatcher, $frontPath)
39
    {
40 8
        $this->engine     = $engine;
41 8
        $this->dispatcher = $dispatcher;
42 8
        $this->frontPath  = $frontPath;
43 8
    }
44
45
    /**
46
     * Set Parameters
47
     *
48
     * @param array $parameters
49
     *
50
     * @return $this
51
     */
52 4
    public function setParameters($parameters)
53
    {
54 4
        $this->parameters = $parameters;
55
56 4
        return $this;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 8
    public function render()
63
    {
64 8
        $event = new BeforeRenderEvent($this->frontPath);
65 8
        $this->dispatcher->dispatch(FrontRenderEvents::BEFORE_RENDER, $event);
66
67 8
        $frontPath = $event->getFrontPath();
68 8
        if (empty($frontPath)) {
69 1
            throw new FrontRenderException('You need to configure a front path.');
70
        }
71
72 7
        return $this->engine->render($frontPath, $this->parameters);
73
    }
74
}
75