Completed
Push — develop ( d2e190...d276d7 )
by Alec
10:43 queued 07:55
created

Factory   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 8.33%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 158
ccs 4
cts 48
cp 0.0833
rs 10
c 0
b 0
f 0
wmc 22

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A setFormatter() 0 9 3
A getSimpleCounterReportFormatter() 0 7 2
A getTimerReportFormatter() 0 7 2
A setSimpleCounterReportFormatter() 0 4 1
A setTimerReportFormatter() 0 3 1
A setExtendedCounterReportFormatter() 0 4 1
A getProfilerReportFormatter() 0 7 2
A setBenchmarkReportFormatter() 0 3 1
A getBenchmarkReportFormatter() 0 7 2
A setBenchmarkFunctionFormatter() 0 4 1
A setProfilerReportFormatter() 0 3 1
A getBenchmarkFunctionFormatter() 0 7 2
A getExtendedCounterReportFormatter() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools\Reports;
4
5
use AlecRabbit\Tools\Reports\Formatters\BenchmarkFunctionFormatter;
6
use AlecRabbit\Tools\Reports\Formatters\BenchmarkReportFormatter;
7
use AlecRabbit\Tools\Reports\Formatters\Contracts\FormatterInterface;
8
use AlecRabbit\Tools\Reports\Formatters\ExtendedCounterReportFormatter;
9
use AlecRabbit\Tools\Reports\Formatters\ProfilerReportFormatter;
10
use AlecRabbit\Tools\Reports\Formatters\SimpleCounterReportFormatter;
11
use AlecRabbit\Tools\Reports\Formatters\TimerReportFormatter;
12
use function AlecRabbit\typeOf;
13
14
class Factory
15
{
16
    /** @var null|TimerReportFormatter */
17
    protected static $timerReportFormatter;
18
19
    /** @var null|SimpleCounterReportFormatter */
20
    protected static $simpleCounterReportFormatter;
21
22
    /** @var null|ExtendedCounterReportFormatter */
23
    protected static $extendedCounterReportFormatter;
24
25
    /** @var null|ProfilerReportFormatter */
26
    protected static $profilerReportFormatter;
27
28
    /** @var null|BenchmarkReportFormatter */
29
    protected static $benchmarkReportFormatter;
30
31
    /** @var null|BenchmarkFunctionFormatter */
32
    protected static $benchmarkFunctionFormatter;
33
34
    /** @codeCoverageIgnore */
35
    private function __construct()
36
    {
37
        // Static class
38
    }
39
40
    /**
41
     * @return TimerReportFormatter
42
     */
43 2
    public static function getTimerReportFormatter(): TimerReportFormatter
44
    {
45 2
        if (null === static::$timerReportFormatter) {
46 1
            static::$timerReportFormatter = new TimerReportFormatter();
47
        }
48
        return
49 2
            static::$timerReportFormatter;
50
    }
51
52
    /**
53
     * @param null|TimerReportFormatter $timerReportFormatter
54
     */
55
    public static function setTimerReportFormatter(?TimerReportFormatter $timerReportFormatter): void
56
    {
57
        self::$timerReportFormatter = $timerReportFormatter;
58
    }
59
60
    /**
61
     * @return SimpleCounterReportFormatter
62
     */
63
    public static function getSimpleCounterReportFormatter(): SimpleCounterReportFormatter
64
    {
65
        if (null === static::$simpleCounterReportFormatter) {
66
            static::$simpleCounterReportFormatter = new SimpleCounterReportFormatter();
67
        }
68
        return
69
            new SimpleCounterReportFormatter();
70
    }
71
72
    /**
73
     * @param null|SimpleCounterReportFormatter $simpleCounterReportFormatter
74
     */
75
    public static function setSimpleCounterReportFormatter(
76
        ?SimpleCounterReportFormatter $simpleCounterReportFormatter
77
    ): void {
78
        self::$simpleCounterReportFormatter = $simpleCounterReportFormatter;
79
    }
80
81
    /**
82
     * @return ExtendedCounterReportFormatter
83
     */
84
    public static function getExtendedCounterReportFormatter(): ExtendedCounterReportFormatter
85
    {
86
        if (null === static::$extendedCounterReportFormatter) {
87
            static::$extendedCounterReportFormatter = new ExtendedCounterReportFormatter();
88
        }
89
        return
90
            new ExtendedCounterReportFormatter();
91
    }
92
93
    /**
94
     * @param null|ExtendedCounterReportFormatter $extendedCounterReportFormatter
95
     */
96
    public static function setExtendedCounterReportFormatter(
97
        ?ExtendedCounterReportFormatter $extendedCounterReportFormatter
98
    ): void {
99
        self::$extendedCounterReportFormatter = $extendedCounterReportFormatter;
100
    }
101
102
    /**
103
     * @return BenchmarkFunctionFormatter
104
     */
105
    public static function getBenchmarkFunctionFormatter(): BenchmarkFunctionFormatter
106
    {
107
        if (null === static::$benchmarkFunctionFormatter) {
108
            static::$benchmarkFunctionFormatter = new BenchmarkFunctionFormatter();
109
        }
110
        return
111
            static::$benchmarkFunctionFormatter->resetEqualReturns();
112
    }
113
114
    /**
115
     * @param BenchmarkFunctionFormatter $benchmarkFunctionFormatter
116
     */
117
    public static function setBenchmarkFunctionFormatter(
118
        BenchmarkFunctionFormatter $benchmarkFunctionFormatter
119
    ): void {
120
        self::$benchmarkFunctionFormatter = $benchmarkFunctionFormatter;
121
    }
122
123
    /**
124
     * @return BenchmarkReportFormatter
125
     */
126
    public static function getBenchmarkReportFormatter(): BenchmarkReportFormatter
127
    {
128
        if (null === static::$benchmarkReportFormatter) {
129
            static::$benchmarkReportFormatter = new BenchmarkReportFormatter();
130
        }
131
        return
132
            new BenchmarkReportFormatter();
133
    }
134
135
    /**
136
     * @param null|BenchmarkReportFormatter $benchmarkReportFormatter
137
     */
138
    public static function setBenchmarkReportFormatter(?BenchmarkReportFormatter $benchmarkReportFormatter): void
139
    {
140
        self::$benchmarkReportFormatter = $benchmarkReportFormatter;
141
    }
142
143
    /**
144
     * @return ProfilerReportFormatter
145
     */
146
    public static function getProfilerReportFormatter(): ProfilerReportFormatter
147
    {
148
        if (null === static::$profilerReportFormatter) {
149
            static::$profilerReportFormatter = new ProfilerReportFormatter();
150
        }
151
        return
152
            new ProfilerReportFormatter();
153
    }
154
155
    /**
156
     * @param null|ProfilerReportFormatter $profilerReportFormatter
157
     */
158
    public static function setProfilerReportFormatter(?ProfilerReportFormatter $profilerReportFormatter): void
159
    {
160
        self::$profilerReportFormatter = $profilerReportFormatter;
161
    }
162
163
    public function setFormatter(FormatterInterface $formatter): void
164
    {
165
        if ($formatter instanceof TimerReportFormatter) {
166
            static::setTimerReportFormatter($formatter);
167
        }
168
        if ($formatter instanceof ProfilerReportFormatter) {
169
            static::setProfilerReportFormatter($formatter);
170
        }
171
        throw new \RuntimeException('Formatter [' . typeOf($formatter) .'] is not accepted.');
172
    }
173
}
174