TimerTaskDecorator::callbackAfter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\DeployerTimer;
5
6
class TimerTaskDecorator implements TaskDecorator
7
{
8
    /**
9
     * @var TimerEvents
10
     */
11
    private $events;
12
13
    /**
14
     * @var float[]
15
     */
16
    private $startTimeStack = [];
17
18
    /**
19
     * @var Clock
20
     */
21
    private $clock;
22
23 9
    public function __construct(?Clock $clock = null)
24
    {
25 9
        $this->events = new TimerEvents();
26 9
        $this->clock = $clock ?? new SystemClock();
27 9
    }
28
29 3
    public function name(): string
30
    {
31 3
        return 'timer';
32
    }
33
34 9
    public function callbackBefore(string $taskName): callable
35
    {
36
        return function () use ($taskName) {
37 9
            \array_push($this->startTimeStack, $this->clock->microtime());
38 9
            $this->events->append(new TimerEvent(TimerEvent::TYPE_BEGIN, $taskName, $this->clock->microtime(), 0));
39 9
        };
40
    }
41
42 9
    public function callbackAfter(string $taskName): callable
43
    {
44
        return function () use ($taskName) {
45 9
            $startTime = \array_pop($this->startTimeStack);
46 9
            $duration = $this->clock->microtime() - $startTime;
47 9
            $this->events->append(
48 9
                new TimerEvent(TimerEvent::TYPE_END, $taskName, $this->clock->microtime(), $duration)
49
            );
50 9
        };
51
    }
52
53 9
    public function resultsAsCsv(): string
54
    {
55 9
        return $this->events->asCsv();
56
    }
57
}
58