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
|
|
|
*/ |
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 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 Themed |
85
|
|
|
* @throws InvalidStyleException |
86
|
|
|
*/ |
87
|
12 |
|
public static function getThemedObject(): Themed |
88
|
|
|
{ |
89
|
12 |
|
if (null === static::$theme) { |
90
|
1 |
|
static::$theme = new Themed(static::$colour); |
91
|
|
|
} |
92
|
12 |
|
return static::$theme; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param bool $colour |
97
|
|
|
*/ |
98
|
2 |
|
public static function setColour(bool $colour): void |
99
|
|
|
{ |
100
|
2 |
|
self::$colour = $colour; |
101
|
2 |
|
} |
102
|
|
|
} |
103
|
|
|
|