ReportWriteManager::write()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
namespace Mathielen\ReportWriteEngine;
3
4
use Assert\Assertion;
5
use Mathielen\ReportWriteEngine\Engine\ReportConfig;
6
use Mathielen\ReportWriteEngine\Engine\Writer\ReportWriterInterface;
7
8
class ReportWriteManager
9
{
10
11
    /**
12
     * @var ReportWriterInterface[]
13
     */
14
    private $reportWriters;
15
16
    public function __construct(array $reportWriters = [])
17
    {
18
        $this->reportWriters = $reportWriters;
19
    }
20
21
    /**
22
     * @return $this
23
     */
24
    public function addReportWriter($format, ReportWriterInterface $reportWriter)
25
    {
26
        Assertion::string($format);
27
        Assertion::notEmpty($format);
28
29
        $this->reportWriters[$format] = $reportWriter;
30
31
        return $this;
32
    }
33
34
    /**
35
     * @return ReportWriterInterface
36
     */
37
    private function getReportWriter($format)
38
    {
39
        Assertion::string($format);
40
        Assertion::notEmpty($format);
41
42
        if (!isset($this->reportWriters[$format])) {
43
            throw new \RuntimeException("ReportWriter for $format not found");
44
        }
45
46
        return $this->reportWriters[$format];
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function write($format, array $data, ReportConfig $reportConfig)
53
    {
54
        $reportWriter = $this->getReportWriter($format);
55
56
        return $reportWriter->write($data, $reportConfig);
57
    }
58
59
}
60