Passed
Push — master ( 4bff04...8ad0e5 )
by Alec
03:06
created

Factory::getThemeObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
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
    /** @var Theme */
27
    protected static $theme;
28
    /** @var bool */
29
    protected static $colour = false;
30
31
    /**
32
     * @param ReportableInterface $reportable
33
     * @return ReportInterface
34
     * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
35
     */
36 12
    public static function makeReport(ReportableInterface $reportable): ReportInterface
37
    {
38 12
        if ($reportable instanceof Timer) {
39
            return
40 9
                new TimerReport($reportable);
41
        }
42 10
        if ($reportable instanceof Counter) {
43
            return
44 9
                new CounterReport($reportable);
45
        }
46 8
        if ($reportable instanceof Profiler) {
47
            return
48 7
                new ProfilerReport($reportable);
49
        }
50 4
        if ($reportable instanceof Benchmark) {
51
            return
52 3
                new BenchmarkReport($reportable);
53
        }
54 1
        throw new \RuntimeException('Attempt to create unimplemented report for: ' . typeOf($reportable));
55
    }
56
57
    /**
58
     * @param ReportInterface $report
59
     * @return ReportFormatter
60
     * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
61
     */
62 13
    public static function makeFormatter(ReportInterface $report): ReportFormatter
63
    {
64 13
        if ($report instanceof TimerReport) {
65
            return
66 10
                new TimerReportFormatter($report);
67
        }
68 10
        if ($report instanceof CounterReport) {
69
            return
70 9
                new CounterReportFormatter($report);
71
        }
72 8
        if ($report instanceof ProfilerReport) {
73
            return
74 7
                new ProfilerReportFormatter($report);
75
        }
76 5
        if ($report instanceof BenchmarkReport) {
77
            return
78 4
                new BenchmarkReportFormatter($report);
79
        }
80 1
        throw new \RuntimeException('Attempt to create unimplemented formatter for: ' . typeOf($report));
81
    }
82
83
    /**
84
     * @return Theme
85
     */
86 12
    public static function getThemeObject(): Theme
87
    {
88 12
        if (null === static::$theme) {
89 1
            static::$theme = new Theme(static::$colour);
90
        }
91 12
        return static::$theme;
92
    }
93
94
    /**
95
     * @param bool $colour
96
     */
97 2
    public static function setColour(bool $colour): void
98
    {
99 2
        self::$colour = $colour;
100 2
    }
101
}
102