Passed
Push — master ( d0d18f...f9b1f4 )
by Alec
12:28
created

MemoryUsageReport::prepareString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Accessories\MemoryUsage;
4
5
use AlecRabbit\Formatters\Contracts\FormatterInterface;
6
use AlecRabbit\Reports\Core\AbstractReport;
7
use AlecRabbit\Reports\Core\AbstractReportable;
8
9
class MemoryUsageReport extends AbstractReport implements MemoryUsageReportInterface
10
{
11
    /** @var int */
12
    protected $usage;
13
14
    /** @var int */
15
    protected $peakUsage;
16
17
    /** @var int */
18
    protected $usageReal;
19
20
    /** @var int */
21
    protected $peakUsageReal;
22
23
    /** @var int */
24
    protected $usageDiff;
25
26
    /** @var int */
27
    protected $peakUsageDiff;
28
29
    /** @var int */
30
    protected $usageRealDiff;
31
32
    /** @var int */
33
    protected $peakUsageRealDiff;
34
35
    /** @var null|MemoryUsageReportFormatter */
36
    protected $formatter;
37
38
    /**
39
     * MemoryUsageReport constructor.
40
     * @param int $usage
41
     * @param int $peakUsage
42
     * @param int $usageReal
43
     * @param int $peakUsageReal
44
     * @param FormatterInterface|null $formatter
45
     * @param AbstractReportable|null $reportable
46
     */
47 8
    public function __construct(
48
        int $usage = null,
49
        int $peakUsage = null,
50
        int $usageReal = null,
51
        int $peakUsageReal = null,
52
        FormatterInterface $formatter = null,
53
        AbstractReportable $reportable = null
54
    ) {
55 8
        parent::__construct($formatter, $reportable);
56 8
        $this->usage = $usage ?? memory_get_usage();
57 8
        $this->peakUsage = $peakUsage ?? memory_get_peak_usage();
58 8
        $this->usageReal = $usageReal ?? memory_get_usage(true);
59 8
        $this->peakUsageReal = $peakUsageReal ?? memory_get_peak_usage(true);
60 8
        $this->usageDiff = $this->usage;
61 8
        $this->peakUsageDiff = $this->peakUsage;
62 8
        $this->usageRealDiff = $this->usageReal;
63 8
        $this->peakUsageRealDiff = $this->peakUsageReal;
64 8
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 6
    public function getUsage(): int
70
    {
71 6
        return $this->usage;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 6
    public function getPeakUsage(): int
78
    {
79 6
        return $this->peakUsage;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 6
    public function getUsageReal(): int
86
    {
87 6
        return $this->usageReal;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 6
    public function getPeakUsageReal(): int
94
    {
95 6
        return $this->peakUsageReal;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 3
    public function getUsageDiff(): int
102
    {
103 3
        return $this->usageDiff;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 3
    public function getPeakUsageDiff(): int
110
    {
111 3
        return $this->peakUsageDiff;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 3
    public function getUsageRealDiff(): int
118
    {
119 3
        return $this->usageRealDiff;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 3
    public function getPeakUsageRealDiff(): int
126
    {
127 3
        return $this->peakUsageRealDiff;
128
    }
129
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 2
    public function getUsageString(?string $unit = null, ?int $decimals = null): string
135
    {
136 2
        return $this->prepareString($this->usage, $unit, $decimals);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 2
    public function getPeakUsageString(?string $unit = null, ?int $decimals = null): string
143
    {
144 2
        return $this->prepareString($this->peakUsage, $unit, $decimals);
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 2
    public function getUsageRealString(?string $unit = null, ?int $decimals = null): string
151
    {
152 2
        return $this->prepareString($this->usageReal, $unit, $decimals);
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 2
    public function getPeakUsageRealString(?string $unit = null, ?int $decimals = null): string
159
    {
160 2
        return $this->prepareString($this->peakUsageReal, $unit, $decimals);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 1
    public function getUsageDiffString(?string $unit = null, ?int $decimals = null): string
167
    {
168 1
        return $this->prepareString($this->usageDiff, $unit, $decimals);
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 1
    public function getPeakUsageDiffString(?string $unit = null, ?int $decimals = null): string
175
    {
176 1
        return $this->prepareString($this->peakUsageDiff, $unit, $decimals);
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 1
    public function getUsageRealDiffString(?string $unit = null, ?int $decimals = null): string
183
    {
184 1
        return $this->prepareString($this->usageRealDiff, $unit, $decimals);
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190 1
    public function getPeakUsageRealDiffString(?string $unit = null, ?int $decimals = null): string
191
    {
192 1
        return $this->prepareString($this->peakUsageRealDiff, $unit, $decimals);
193
    }
194
195
    /**
196
     * @param int $forValue
197
     * @param null|string $unit
198
     * @param null|int $decimals
199
     * @return string
200
     */
201 2
    protected function prepareString(int $forValue, ?string $unit = null, ?int $decimals = null): string
202
    {
203 2
        if ($this->formatter instanceof MemoryUsageReportFormatter) {
204
            return
205 2
                $this->formatter->getString($forValue, $unit, $decimals);
206
        }
207
        // @codeCoverageIgnoreStart
208
        return 'WRONG FORMATTER TYPE: ' . get_class($this->formatter);
209
        // @codeCoverageIgnoreEnd
210
    }
211
212 2
    public function diff(MemoryUsageReport $firstReport): self
213
    {
214 2
        $this->usageDiff         = $this->usage - $firstReport->usage;
215 2
        $this->peakUsageDiff     = $this->peakUsage - $firstReport->peakUsage;
216 2
        $this->usageRealDiff     = $this->usageReal - $firstReport->usageReal;
217 2
        $this->peakUsageRealDiff = $this->peakUsageReal - $firstReport->peakUsageReal;
218 2
        return $this;
219
    }
220
}
221