Completed
Push — develop ( fe1195...b733eb )
by Alec
05:48
created

Factory::makeFormatter()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 19
ccs 9
cts 10
cp 0.9
crap 5.025
rs 9.5222
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 01.12.18
5
 * Time: 17:14
6
 */
7
8
namespace AlecRabbit\Tools\Reports;
9
10
use AlecRabbit\Tools\Benchmark;
11
use AlecRabbit\Tools\Counter;
12
use AlecRabbit\Tools\Internal\Theme;
13
use AlecRabbit\Tools\Profiler;
14
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface;
15
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
16
use AlecRabbit\Tools\Reports\Formatters\BenchmarkReportFormatter;
17
use AlecRabbit\Tools\Reports\Formatters\Contracts\ReportFormatter;
18
use AlecRabbit\Tools\Reports\Formatters\CounterReportFormatter;
19
use AlecRabbit\Tools\Reports\Formatters\ProfilerReportFormatter;
20
use AlecRabbit\Tools\Reports\Formatters\TimerReportFormatter;
21
use AlecRabbit\Tools\Timer;
22
use function AlecRabbit\typeOf;
23
24
class Factory
25
{
26
    private static $theme;
27
    protected static $colour = false;
28
29
    /**
30
     * @param ReportableInterface $reportable
31
     * @return ReportInterface
32
     */
33 10
    public static function makeReport(ReportableInterface $reportable): ReportInterface
34
    {
35 10
        if ($reportable instanceof Timer) {
36
            return
37 8
                new TimerReport($reportable);
38
        }
39 8
        if ($reportable instanceof Counter) {
40
            return
41 7
                new CounterReport($reportable);
42
        }
43 7
        if ($reportable instanceof Profiler) {
44
            return
45 6
                new ProfilerReport($reportable);
46
        }
47 3
        if ($reportable instanceof Benchmark) {
48
            return
49 2
                new BenchmarkReport($reportable);
50
        }
51 1
        throw new \RuntimeException('Attempt to create unimplemented report for: ' . typeOf($reportable));
52
    }
53
54
    /**
55
     * @param ReportInterface $report
56
     * @return ReportFormatter
57
     */
58 9
    public static function makeFormatter(ReportInterface $report): ReportFormatter
59
    {
60 9
        if ($report instanceof TimerReport) {
61
            return
62 8
                new TimerReportFormatter($report);
63
        }
64 7
        if ($report instanceof CounterReport) {
65
            return
66 7
                new CounterReportFormatter($report);
67
        }
68 6
        if ($report instanceof ProfilerReport) {
69
            return
70 6
                new ProfilerReportFormatter($report);
71
        }
72 3
        if ($report instanceof BenchmarkReport) {
73
            return
74 3
                new BenchmarkReportFormatter($report);
75
        }
76
        throw new \RuntimeException('Attempt to create unimplemented formatter for: ' . typeOf($report));
77
    }
78
79 9
    public static function getThemeObject(): Theme
80
    {
81 9
        if (!static::$theme) {
0 ignored issues
show
Bug introduced by
Since $theme is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $theme to at least protected.
Loading history...
82 1
            static::$theme = new Theme(static::$colour);
83
        }
84 9
        return static::$theme;
85
    }
86
87
    /**
88
     * @param bool $colour
89
     */
90
    public static function setColour(bool $colour): void
91
    {
92
        self::$colour = $colour;
93
    }
94
}
95