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

ReporterFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

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