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

ReporterFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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