TimerTaskDecorator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 2
b 0
f 0
dl 0
loc 50
ccs 18
cts 18
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A callbackAfter() 0 7 1
A name() 0 3 1
A resultsAsCsv() 0 3 1
A callbackBefore() 0 5 1
A __construct() 0 4 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