Completed
Pull Request — master (#235)
by Kévin
04:08 queued 20s
created

ReporterFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 1
A __construct() 0 6 2
A registerFormatter() 0 4 1
A getReporter() 0 8 2
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