Manager::addTime()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.005

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 1
dl 0
loc 28
ccs 16
cts 17
cp 0.9412
crap 5.005
rs 9.1608
c 0
b 0
f 0
1
<?php
2
/*
3
    Condorcet PHP - Election manager and results calculator.
4
    Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.
5
6
    By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt)
7
    https://github.com/julien-boudry/Condorcet
8
*/
9
declare(strict_types=1);
10
11
namespace CondorcetPHP\Condorcet\Timer;
12
13
use CondorcetPHP\Condorcet\CondorcetVersion;
14
use CondorcetPHP\Condorcet\Throwable\CondorcetException;
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 96
    public function addTime ( Chrono $chrono ) : void
27
    {
28 96
        if ($chrono->getTimerManager() === $this) :
29 96
            if ($this->_lastChronoTimestamp === null && $chrono->getStart() !== $this->_startDeclare) :
30
                return;
31
            endif;
32
            
33 96
            $m = microtime(true);
34
35 96
            if ( $this->_lastChronoTimestamp > $chrono->getStart() ) :
36 88
                $c = $this->_lastChronoTimestamp;
37
            else :
38 96
                $c = $chrono->getStart();
39 96
                $this->_history[] = [   'role' => $chrono->getRole(),
40 96
                                        'process_in' => ($m - $c),
41 96
                                        'timer_start' => $c,
42 96
                                        'timer_end' => $m
43
                                    ];
44
            endif;
45
46 96
            $this->_globalTimer += ($m - $c);
47
48 96
            $this->_lastTimer = ($m - $chrono->getStart());
49 96
            $this->_lastChronoTimestamp = $m;
50
        else :
51 1
            throw new CondorcetException (0, 'Only chrono linked to this Manager can be used');
52
        endif;
53 96
    }
54
55 2
    public function getGlobalTimer () : float
56
    {
57 2
        return $this->_globalTimer;
58
    }
59
60 2
    public function getLastTimer () : float
61
    {
62 2
        return $this->_lastTimer;
63
    }
64
65 1
    public function getHistory () : array
66
    {
67 1
        return $this->_history;
68
    }
69
70 96
    public function startDeclare (Chrono $chrono) : void
71
    {
72 96
        if ($this->_startDeclare === null) :
73 96
            $this->_startDeclare = $chrono->getStart();
74
        endif;
75 96
    }
76
}
77