Completed
Pull Request — master (#235)
by Kévin
03:13
created

ReporterFactory::getReporter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Report;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use PHPSA\Report\Formatter;
7
8
class ReporterFactory
9
{
10
    protected $formatters = [];
11
12
    public static function create()
13
    {
14
        return new self([
15
            'json' => new Formatter\JsonFormatter(),
16
            'text' => new Formatter\TextFormatter(),
17
        ]);
18
    }
19
20
    public function __construct(array $formatters = [])
21
    {
22
        foreach ($formatters as $format => $formatter) {
23
            $this->registerFormatter($format, $formatter);
24
        }
25
    }
26
27
    /**
28
     * @param string $format
29
     * @param Formatter\ReportFormatter $formatter
30
     */
31
    public function registerFormatter($format, Formatter\ReportFormatter $formatter)
32
    {
33
        $this->formatters[$format] = $formatter;
34
    }
35
36
    /**
37
     * @param string $format
38
     * @param OutputInterface $output
39
     *
40
     * @return Reporter
41
     *
42
     * @throws \LogicException If no reporter is registered for the given format.
43
     */
44
    public function getReporter($format, OutputInterface $output)
45
    {
46
        if (array_key_exists($format, $this->formatters)) {
47
            return new Reporter($this->formatters[$format], $output);
48
        }
49
50
        throw new \LogicException(sprintf('No reporter registered for format "%s".', $format));
51
    }
52
}
53