Passed
Branch dev-2.0 (a2c735)
by Boudry
03:06
created

Manager::getHistory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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
14
use CondorcetPHP\Condorcet\CondorcetException;
15
use CondorcetPHP\Condorcet\CondorcetVersion;
16
17
class Manager
18
{
19
    use CondorcetVersion;
20
21
    protected $_globalTimer = 0.0;
22
    protected $_lastTimer;
23
    protected $_lastChronoTimestamp = null;
24
    protected $_startDeclare = null;
25
    protected $_history = [];
26
27 94
    public function addTime ( Chrono $chrono ) : void
28
    {
29 94
        if ($chrono->getTimerManager() === $this) :
30 94
            if ($this->_lastChronoTimestamp === null && $chrono->getStart() !== $this->_startDeclare) :
31
                return;
32
            endif;
33
            
34 94
            $m = microtime(true);
35
36 94
            if ( $this->_lastChronoTimestamp > $chrono->getStart() ) :
37 86
                $c = $this->_lastChronoTimestamp;
38
            else :
39 94
                $c = $chrono->getStart();
40 94
                $this->_history[] = [   'role' => $chrono->getRole(),
41 94
                                        'process_in' => ($m - $c),
42 94
                                        'timer_start' => $c,
43 94
                                        'timer_end' => $m
44
                                    ];
45
            endif;
46
47 94
            $this->_globalTimer += ($m - $c);
48
49 94
            $this->_lastTimer = ($m - $chrono->getStart());
50 94
            $this->_lastChronoTimestamp = $m;
51
        else :
52 1
            throw new CondorcetException (0, 'Only chrono linked to this Manager can be used');
53
        endif;
54 94
    }
55
56 2
    public function getGlobalTimer () : float
57
    {
58 2
        return $this->_globalTimer;
59
    }
60
61 2
    public function getLastTimer () : float
62
    {
63 2
        return $this->_lastTimer;
64
    }
65
66 1
    public function getHistory () : array
67
    {
68 1
        return $this->_history;
69
    }
70
71 94
    public function startDeclare (Chrono $chrono) : void
72
    {
73 94
        if ($this->_startDeclare === null) {
74 94
            $this->_startDeclare = $chrono->getStart();
75
        }
76 94
    }
77
}
78