Passed
Pull Request — master (#4)
by Kevin
07:28
created

RunContext::getMemory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule;
4
5
use Symfony\Component\Console\Helper\Helper;
6
7
/**
8
 * @author Kevin Bond <[email protected]>
9
 */
10
abstract class RunContext
11
{
12
    private $startTime;
13
    private $duration;
14
    private $memory;
15
16 76
    public function __construct()
17
    {
18 76
        $this->startTime = \time();
19 76
    }
20
21
    abstract public function __toString(): string;
22
23 34
    final public function startTime(): int
24
    {
25 34
        return $this->startTime;
26
    }
27
28 34
    final public function hasRun(): bool
29
    {
30 34
        return null !== $this->memory;
31
    }
32
33 12
    final public function getDuration(): int
34
    {
35 12
        $this->ensureHasRun();
36
37 12
        return $this->duration;
38
    }
39
40 12
    final public function getFormattedDuration(): string
41
    {
42 12
        return Helper::formatTime($this->getDuration());
0 ignored issues
show
Bug Best Practice introduced by
The expression return Symfony\Component...e($this->getDuration()) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
43
    }
44
45 12
    final public function getMemory(): int
46
    {
47 12
        $this->ensureHasRun();
48
49 12
        return $this->memory;
50
    }
51
52 12
    final public function getFormattedMemory(): string
53
    {
54 12
        return Helper::formatMemory($this->getMemory());
55
    }
56
57 32
    final protected function markAsRun(int $memory): void
58
    {
59 32
        $this->duration = \time() - $this->startTime();
60 32
        $this->memory = $memory;
61 32
    }
62
63 34
    final protected function ensureHasRun(): void
64
    {
65 34
        if (!$this->hasRun()) {
66 2
            throw new \LogicException("\"{$this}\" has not yet run.");
67
        }
68 32
    }
69
}
70