ReportWriteManager   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 52
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addReportWriter() 0 9 1
A getReportWriter() 0 11 2
A write() 0 6 1
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