Completed
Push — master ( 922505...49fe4c )
by Boudry
05:20
created

Candidate::getProvisionalState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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;
11
12
use Condorcet\Condorcet;
13
use Condorcet\CondorcetException;
14
use Condorcet\CondorcetVersion;
15
use Condorcet\Linkable;
16
17
class Candidate
18
{
19
    use Linkable, CondorcetVersion;
20
21
    private $_name = [];
22
    private $_provisional = false;
23
24
        ///
25
26 88
    public function __construct (string $name)
27
    {
28 88
        $this->setName($name);
29 88
    }
30
31 85
    public function __toString () : string
32
    {
33 85
        return $this->getName();
34
    }
35
36
        ///
37
38
    // SETTERS
39
40 88
    public function setName (string $name) : bool
41
    {
42 88
        $name = trim($name);
43
44 88
        if (mb_strlen($name) > Election::MAX_LENGTH_CANDIDATE_ID ) :
45 1
            throw new CondorcetException(1, $name);
46
        endif;
47
48 88
        if (!$this->checkName($name)) :
49 1
            throw new CondorcetException(19, $name);
50
        endif;
51
52 88
        $this->_name[] =  [ 'name' => $name, 'timestamp' => microtime(true) ];
53
54 88
        return true;
55
    }
56
57 84
    public function setProvisionalState (bool $provisional) : bool
58
    {
59 84
        $this->_provisional = $provisional;
60 84
        return true;
61
    }
62
63
    // GETTERS
64
65 86
    public function getName () : string
66
    {
67 86
        return end($this->_name)['name'];
68
    }
69
70 3
    public function getHistory () : array
71
    {
72 3
        return $this->_name;
73
    }
74
75 2
    public function getCreateTimestamp () : float
76
    {
77 2
        return $this->_name[0]['timestamp'];
78
    }
79
80 2
    public function getTimestamp () : float
81
    {
82 2
        return end($this->_name)['timestamp'];
83
    }
84
85 76
    public function getProvisionalState () : bool
86
    {
87 76
        return $this->_provisional;
88
    }
89
90
        ///
91
92
    // INTERNAL
93
94 88
    private function checkName (string $name) : bool
95
    {
96 88
        foreach ($this->_link as &$link) :
97 3
            if (!$link->canAddCandidate($name)) :
98 3
                return false;
99
            endif;
100
        endforeach;
101
102 88
        return true;
103
    }
104
}
105