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