Passed
Branch TEST/ScrutinizerPHPAnalysisEng... (bc3f13)
by Boudry
07:08
created

Manager::addTime()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0488

Importance

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