Passed
Push — master ( 9cc6d5...58bad8 )
by Boudry
02:51
created

Manager::addTime()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0406

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 15
cts 17
cp 0.8824
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 4
nop 1
crap 5.0406
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 5
    public function addTime ( Chrono $chrono )
27
    {
28 5
        if ($this->_lastChronoTimestamp === null && $chrono->getStart() !== $this->_startDeclare) :
29
            return;
30
        endif;
31
32 5
        if ($chrono->getTimerManager() === $this) :
33 5
            $m = microtime(true);
34
35 5
            if ( $this->_lastChronoTimestamp > $chrono->getStart() ) :
36 5
                $c = $this->_lastChronoTimestamp;
37
            else :
38 5
                $c = $chrono->getStart();
39 5
                $this->_history[] = [   'role' => $chrono->getRole(),
40 5
                                        'process_in' => ($m - $c),
41 5
                                        'timer_start' => $c,
42 5
                                        'timer_end' => $m
43
                                    ];
44
            endif;
45
46 5
            $this->_globalTimer += ($m - $c);
47
48 5
            $this->_lastTimer = ($m - $chrono->getStart());
49 5
            $this->_lastChronoTimestamp = $m;
50
        else :
51
            throw new CondorcetException (0, 'Only chrono linked to this Manager can be used');
52
        endif;
53 5
    }
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 5
    public function startDeclare (Chrono $chrono)
71
    {
72 5
        if ($this->_startDeclare === null) {
73 5
            $this->_startDeclare = $chrono->getStart();
74
        }
75 5
    }
76
}
77