Completed
Push — master ( 8ad0e5...05ab4d )
by Alec
04:24
created

Factory::getThemedObject()   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\Exception\InvalidStyleException;
11
use AlecRabbit\Themed;
12
use AlecRabbit\Tools\Benchmark;
13
use AlecRabbit\Tools\Counter;
14
use AlecRabbit\Tools\Profiler;
15
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface;
16
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
17
use AlecRabbit\Tools\Reports\Formatters\BenchmarkReportFormatter;
18
use AlecRabbit\Tools\Reports\Formatters\Contracts\ReportFormatter;
19
use AlecRabbit\Tools\Reports\Formatters\CounterReportFormatter;
20
use AlecRabbit\Tools\Reports\Formatters\ProfilerReportFormatter;
21
use AlecRabbit\Tools\Reports\Formatters\TimerReportFormatter;
22
use AlecRabbit\Tools\Timer;
23
use function AlecRabbit\typeOf;
24
25
class Factory
26
{
27
    /** @var Themed */
28
    protected static $theme;
29
    /** @var bool */
30
    protected static $colour = false;
31
32
    /**
33
     * @param ReportableInterface $reportable
34
     * @return ReportInterface
35
     * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
36
     */
37 12
    public static function makeReport(ReportableInterface $reportable): ReportInterface
38
    {
39 12
        if ($reportable instanceof Timer) {
40
            return
41 9
                new TimerReport($reportable);
42
        }
43 10
        if ($reportable instanceof Counter) {
44
            return
45 9
                new CounterReport($reportable);
46
        }
47 8
        if ($reportable instanceof Profiler) {
48
            return
49 7
                new ProfilerReport($reportable);
50
        }
51 4
        if ($reportable instanceof Benchmark) {
52
            return
53 3
                new BenchmarkReport($reportable);
54
        }
55 1
        throw new \RuntimeException('Attempt to create unimplemented report for: ' . typeOf($reportable));
56
    }
57
58
    /**
59
     * @param ReportInterface $report
60
     * @return ReportFormatter
61
     * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
62
     */
63 13
    public static function makeFormatter(ReportInterface $report): ReportFormatter
64
    {
65 13
        if ($report instanceof TimerReport) {
66
            return
67 10
                new TimerReportFormatter($report);
68
        }
69 10
        if ($report instanceof CounterReport) {
70
            return
71 9
                new CounterReportFormatter($report);
72
        }
73 8
        if ($report instanceof ProfilerReport) {
74
            return
75 7
                new ProfilerReportFormatter($report);
76
        }
77 5
        if ($report instanceof BenchmarkReport) {
78
            return
79 4
                new BenchmarkReportFormatter($report);
80
        }
81 1
        throw new \RuntimeException('Attempt to create unimplemented formatter for: ' . typeOf($report));
82
    }
83
84
    /**
85
     * @return Themed
86
     * @throws InvalidStyleException
87
     */
88 12
    public static function getThemedObject(): Themed
89
    {
90 12
        if (null === static::$theme) {
91 1
            static::$theme = new Themed(static::$colour);
92
        }
93 12
        return static::$theme;
94
    }
95
96
    /**
97
     * @param bool $colour
98
     */
99 2
    public static function setColour(bool $colour): void
100
    {
101 2
        self::$colour = $colour;
102 2
    }
103
}
104