Passed
Branch dev-1.5.x (fb93e4)
by Boudry
04:14
created

Manager::addTime()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0061

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 16
cp 0.9375
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 4
nop 1
crap 5.0061
1
<?php
2
/*
3
    Condorcet PHP Class, with Schulze Methods and others !
4
5
    By Julien Boudry - MIT LICENSE (Please read LICENSE.txt)
6
    https://github.com/julien-boudry/Condorcet
7
*/
8
declare(strict_types=1);
9
10
namespace Condorcet\Timer;
11
12
use Condorcet\Timer\Chrono;
13
use Condorcet\CondorcetException;
14
use Condorcet\CondorcetVersion;
15
16
class Manager
17
{
18
    use CondorcetVersion;
19
20
    protected $_globalTimer = 0.0;
21
    protected $_lastTimer;
22
    protected $_lastChronoTimestamp = null;
23
    protected $_startDeclare = null;
24
    protected $_history = [];
25
26 4
    public function addTime ( Chrono $chrono )
27
    {
28 4
        if ($this->_lastChronoTimestamp === null && $chrono->getStart() !== $this->_startDeclare) :
29 3
            return;
30
        endif;
31
32 4
        if ($chrono->getTimerManager() === $this) :
33 4
            $m = microtime(true);
34
35 4
            if ( $this->_lastChronoTimestamp > $chrono->getStart() ) :
36 4
                $c = $this->_lastChronoTimestamp;
37
            else :
38 4
                $c = $chrono->getStart();
39 4
                $this->_history[] = [   'role' => $chrono->getRole(),
40 4
                                        'process_in' => ($m - $c),
41 4
                                        'timer_start' => $c,
42 4
                                        'timer_end' => $m
43
                                    ];
44
            endif;
45
46 4
            $this->_globalTimer += ($m - $c);
47
48 4
            $this->_lastTimer = ($m - $chrono->getStart());
49 4
            $this->_lastChronoTimestamp = $m;
50
        else :
51
            throw new CondorcetException (0, 'Only chrono linked to this Manager can be used');
52
        endif;
53 4
    }
54
55 1
    public function getGlobalTimer (bool $float = false)
56
    {
57 1
        return ($float) ? $this->_globalTimer : number_format($this->_globalTimer, 5);
58
    }
59
60 2
    public function getLastTimer (bool $float = false)
61
    {
62 2
        return ($float || $this->_lastTimer === null) ? $this->_lastTimer : number_format($this->_lastTimer, 5);
63
    }
64
65 1
    public function getHistory () : array
66
    {
67 1
        return $this->_history;
68
    }
69
70 4
    public function startDeclare (Chrono $chrono)
71
    {
72 4
        if ($this->_startDeclare === null) {
73 4
            $this->_startDeclare = $chrono->getStart();
74
        }
75 4
    }
76
}
77