QueueTimer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 125
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A time() 0 11 1
A saveTime() 0 13 2
A getTime() 0 13 2
A getTimes() 0 7 1
A getLatestTime() 0 6 1
A saveTimes() 0 6 1
A getTimerHandle() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace Rocketeer\Services\Display;
14
15
use Rocketeer\Interfaces\IdentifierInterface;
16
use Rocketeer\Traits\ContainerAwareTrait;
17
18
/**
19
 * Saves the execution time of tasks and
20
 * predicts their future ones.
21
 */
22
class QueueTimer
23
{
24
    use ContainerAwareTrait;
25
26
    //////////////////////////////////////////////////////////////////////
27
    /////////////////////////////// TASKS ////////////////////////////////
28
    //////////////////////////////////////////////////////////////////////
29
30
    /**
31
     * Time a task operation.
32
     *
33
     * @param IdentifierInterface $entity
34
     * @param callable            $callback
35
     *
36
     * @return mixed
37
     */
38 81
    public function time(IdentifierInterface $entity, callable $callback)
39
    {
40
        // Start timer, execute callback, close timer
41 81
        $timerStart = microtime(true);
42 81
        $results = $callback();
43 81
        $time = round(microtime(true) - $timerStart, 4);
44
45 81
        $this->saveTime($entity, $time);
46
47 81
        return $results;
48
    }
49
50
    /**
51
     * Save the execution time of a task for future reference.
52
     *
53
     * @param IdentifierInterface $entity
54
     * @param float               $time
55
     */
56 81
    public function saveTime(IdentifierInterface $entity, $time)
57
    {
58
        // Don't save times in pretend mode
59 81
        if ($this->getOption('pretend')) {
60 49
            return;
61
        }
62
63
        // Append the new time to past ones
64 32
        $past = $this->getTimes($entity);
65 32
        $past[] = $time;
66
67 32
        $this->saveTimes($entity, $past);
68 32
    }
69
70
    /**
71
     * Compute the predicted execution time of a task.
72
     *
73
     * @param IdentifierInterface $entity
74
     *
75
     * @return float|null
76
     */
77 70
    public function getTime(IdentifierInterface $entity)
78
    {
79 70
        $past = $this->getTimes($entity);
80 70
        if (!$past) {
81 69
            return;
82
        }
83
84
        // Compute average time
85 13
        $average = array_sum($past) / count($past);
86 13
        $average = round($average, 2);
87
88 13
        return $average;
89
    }
90
91
    //////////////////////////////////////////////////////////////////////
92
    ////////////////////////// SETTERS/GETTERS ///////////////////////////
93
    //////////////////////////////////////////////////////////////////////
94
95
    /**
96
     * @param IdentifierInterface $entity
97
     *
98
     * @return array
0 ignored issues
show
Documentation introduced by Maxime Fabre
Should the return type not be string|integer|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
99
     */
100 70
    public function getTimes(IdentifierInterface $entity)
101
    {
102 70
        $handle = $this->getTimerHandle($entity);
103 70
        $past = $this->localStorage->get($handle, []);
104
105 70
        return $past;
106
    }
107
108
    /**
109
     * Get the last recorded time.
110
     *
111
     * @param IdentifierInterface $entity
112
     *
113
     * @return float|null
114
     */
115 1
    public function getLatestTime(IdentifierInterface $entity)
116
    {
117 1
        $times = $this->getTimes($entity);
118
119 1
        return end($times);
120
    }
121
122
    /**
123
     * @param IdentifierInterface $entity
124
     * @param float[]             $past
125
     */
126 32
    public function saveTimes(IdentifierInterface $entity, array $past)
127
    {
128 32
        $handle = $this->getTimerHandle($entity);
129
130 32
        $this->localStorage->set($handle, $past);
131 32
    }
132
133
    //////////////////////////////////////////////////////////////////////
134
    ////////////////////////////// HELPERS ///////////////////////////////
135
    //////////////////////////////////////////////////////////////////////
136
137
    /**
138
     * @param IdentifierInterface $entity
139
     *
140
     * @return string
141
     */
142 70
    protected function getTimerHandle(IdentifierInterface $entity)
143
    {
144 70
        return sprintf('times.%s', $entity->getIdentifier());
145
    }
146
}
147