Passed
Push — master ( 9213a8...e174b6 )
by Kevin
02:14
created

RunContext   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 17
c 1
b 0
f 0
dl 0
loc 60
rs 10
ccs 24
cts 24
cp 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormattedMemory() 0 3 1
A markAsRun() 0 4 1
A getDuration() 0 5 1
A getMemory() 0 5 1
A __construct() 0 3 1
A startTime() 0 3 1
A getFormattedDuration() 0 3 1
A hasRun() 0 3 1
A ensureHasRun() 0 4 2
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 78
    public function __construct()
17
    {
18 78
        $this->startTime = \time();
19 78
    }
20
21
    abstract public function __toString(): string;
22
23 36
    final public function startTime(): int
24
    {
25 36
        return $this->startTime;
26
    }
27
28 36
    final public function hasRun(): bool
29
    {
30 36
        return null !== $this->memory;
31
    }
32
33 13
    final public function getDuration(): int
34
    {
35 13
        $this->ensureHasRun();
36
37 13
        return $this->duration;
38
    }
39
40 13
    final public function getFormattedDuration(): string
41
    {
42 13
        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 13
    final public function getMemory(): int
46
    {
47 13
        $this->ensureHasRun();
48
49 13
        return $this->memory;
50
    }
51
52 13
    final public function getFormattedMemory(): string
53
    {
54 13
        return Helper::formatMemory($this->getMemory());
55
    }
56
57 34
    final protected function markAsRun(int $memory): void
58
    {
59 34
        $this->duration = \time() - $this->startTime();
60 34
        $this->memory = $memory;
61 34
    }
62
63
    /**
64
     * @throws \LogicException if has not yet run
65
     */
66 36
    final protected function ensureHasRun(): void
67
    {
68 36
        if (!$this->hasRun()) {
69 2
            throw new \LogicException("\"{$this}\" has not yet run.");
70
        }
71 34
    }
72
}
73