Completed
Push — master ( 60dee0...60dee0 )
by Alec
04:21 queued 02:03
created

Factory::getCounterReportFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Tools\Reports;
6
7
use AlecRabbit\Tools\Benchmark;
8
use AlecRabbit\Tools\Counter;
9
use AlecRabbit\Tools\Internal\BenchmarkFunction;
10
use AlecRabbit\Tools\Profiler;
11
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface;
12
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
13
use AlecRabbit\Tools\Reports\Formatters\BenchmarkFunctionFormatter;
14
use AlecRabbit\Tools\Reports\Formatters\BenchmarkReportFormatter;
15
use AlecRabbit\Tools\Reports\Formatters\Contracts\Formatter;
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 TimerReportFormatter */
25
    protected static $timerReportFormatter;
26
27
    /** @var CounterReportFormatter */
28
    protected static $counterReportFormatter;
29
30
    /** @var ProfilerReportFormatter */
31
    protected static $profilerReportFormatter;
32 15
33
    /** @var BenchmarkReportFormatter */
34 15
    protected static $benchmarkReportFormatter;
35
36 12
    /** @var BenchmarkFunctionFormatter */
37
    protected static $benchmarkFunctionFormatter;
38 10
39
    /** @codeCoverageIgnore */
40 9
    private function __construct()
41
    {
42 8
        // Static class
43
    }
44 7
45
    /**
46 6
     * @param ReportableInterface $reportable
47
     * @return ReportInterface
48 5
     */
49
    public static function makeReport(ReportableInterface $reportable): ReportInterface
50 1
    {
51
        if ($reportable instanceof Timer) {
52
            return
53
                new TimerReport($reportable);
54
        }
55
        if ($reportable instanceof Counter) {
56
            return
57 15
                new CounterReport($reportable);
58
        }
59 15
        if ($reportable instanceof Profiler) {
60
            return
61 12
                new ProfilerReport($reportable);
62
        }
63 10
        if ($reportable instanceof Benchmark) {
64
            return
65 9
                new BenchmarkReport($reportable);
66
        }
67 8
        throw new \RuntimeException('Attempt to create unimplemented report for: ' . typeOf($reportable));
68
    }
69 7
70
    /**
71 6
     * @param ReportInterface $report
72
     * @return Formatter
73 5
     */
74
    public static function makeFormatter(ReportInterface $report): Formatter
75 1
    {
76
        if ($report instanceof TimerReport) {
77
            return
78
                self::getTimerReportFormatter($report);
79
        }
80
        if ($report instanceof CounterReport) {
81
            return
82
                self::getCounterReportFormatter($report);
83
        }
84
        if ($report instanceof ProfilerReport) {
85
            return
86
                self::getProfilerReportFormatter($report);
87
        }
88
        if ($report instanceof BenchmarkReport) {
89
            return
90
                self::getBenchmarkReportFormatter($report);
91
        }
92
        throw new \RuntimeException('Attempt to create unimplemented formatter for: ' . typeOf($report));
93
    }
94
95
    /**
96
     * @param TimerReport $report
97
     * @return TimerReportFormatter
98
     */
99
    protected static function getTimerReportFormatter(TimerReport $report): TimerReportFormatter
100
    {
101
//        if (null === static::$timerReportFormatter) {
102
//            static::$timerReportFormatter = new TimerReportFormatter($report);
103
//        }
104
        return
105
            new TimerReportFormatter($report);
106
    }
107
108
    /**
109
     * @param CounterReport $report
110
     * @return CounterReportFormatter
111
     */
112
    protected static function getCounterReportFormatter(CounterReport $report): CounterReportFormatter
113
    {
114
//        if (null === static::$counterReportFormatter) {
115
//            static::$counterReportFormatter = new CounterReportFormatter($report);
116
//        }
117
        return
118
            new CounterReportFormatter($report);
119
    }
120
121
    /**
122
     * @param ProfilerReport $report
123
     * @return ProfilerReportFormatter
124
     */
125
    protected static function getProfilerReportFormatter(ProfilerReport $report): ProfilerReportFormatter
126
    {
127
//        if (null === static::$profilerReportFormatter) {
128
//            static::$profilerReportFormatter = new ProfilerReportFormatter($report);
129
//        }
130
        return
131
            new ProfilerReportFormatter($report);
132
    }
133
134
    /**
135
     * @param BenchmarkReport $report
136
     * @return BenchmarkReportFormatter
137
     */
138
    protected static function getBenchmarkReportFormatter(BenchmarkReport $report): BenchmarkReportFormatter
139
    {
140
//        if (null === static::$benchmarkReportFormatter) {
141
//            static::$benchmarkReportFormatter = new BenchmarkReportFormatter($report);
142
//        }
143
        return
144
            new BenchmarkReportFormatter($report);
145
    }
146
147
    /**
148
     * @param BenchmarkFunction $function
149
     * @return BenchmarkFunctionFormatter
150
     */
151
    public static function getBenchmarkFunctionFormatter(BenchmarkFunction $function): BenchmarkFunctionFormatter
152
    {
153
//        if (null === static::$benchmarkFunctionFormatter) {
154
//            static::$benchmarkFunctionFormatter = new BenchmarkFunctionFormatter($function);
155
//        }
156
        return new BenchmarkFunctionFormatter($function);
157
    }
158
}
159