RendererRepository::getRendererForNode()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 12
1
<?php
2
namespace Mathielen\ReportWriteEngine\Engine;
3
4
use Mathielen\ReportWriteEngine\Engine\Renderer\RendererInterface;
5
6
class RendererRepository
7
{
8
9
    /**
10
     * @var array
11
     */
12
    private $reportConfig;
13
14
    /**
15
     * @var array
16
     */
17
    private $renderer = [];
18
19
    public function __construct(array $reportConfig = [])
20
    {
21
        $this->reportConfig = $reportConfig;
22
    }
23
24
    /**
25
     * @return $this
26
     */
27
    public function setConfig(array $reportConfig = [])
28
    {
29
        $this->reportConfig = $reportConfig;
30
31
        return $this;
32
    }
33
34
    /**
35
     * @return $this
36
     */
37
    public function add($orientation, RendererInterface $renderer)
38
    {
39
        $this->renderer[$orientation] = $renderer;
40
41
        return $this;
42
    }
43
44
    /**
45
     * @return RendererInterface
46
     */
47
    public function getRendererForNode($nodeId)
48
    {
49
        $orientation = isset($this->reportConfig[$nodeId]) ? $this->reportConfig[$nodeId] : RendererInterface::ORIENTATION_VERTICAL;
50
        if (!isset($this->renderer[$orientation])) {
51
            throw new \RuntimeException("Could not find writer for orientation $orientation");
52
        }
53
54
        return $this->renderer[$orientation];
55
    }
56
57
}
58