Passed
Branch master (73ca69)
by Boudry
03:33
created

Candidate   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 80.65%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 3
dl 0
loc 88
ccs 25
cts 31
cp 0.8065
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
A setName() 0 16 3
A setProvisionalState() 0 5 1
A getName() 0 4 1
A getHistory() 0 4 1
A getCreateTimestamp() 0 4 1
A getTimestamp() 0 4 1
A getProvisionalState() 0 4 1
A checkName() 0 10 3
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 4
    public function __construct (string $name)
27
    {
28 4
        $this->setName($name);
29 4
    }
30
31 4
    public function __toString () : string
32
    {
33 4
        return $this->getName();
34
    }
35
36
        ///
37
38
    // SETTERS
39
40 4
    public function setName (string $name) : bool
41
    {
42 4
        $name = trim($name);
43
44 4
        if (mb_strlen($name) > Election::MAX_LENGTH_CANDIDATE_ID ) :
45
            throw new CondorcetException(1, $name);
46
        endif;
47
48 4
        if (!$this->checkName($name)) :
49
            throw new CondorcetException(19, $name);
50
        endif;
51
52 4
        $this->_name[] =  [ 'name' => $name, 'timestamp' => microtime(true) ];
53
54 4
        return true;
55
    }
56
57 4
    public function setProvisionalState (bool $provisional) : bool
58
    {
59 4
        $this->_provisional = $provisional;
60 4
        return true;
61
    }
62
63
    // GETTERS
64
65 4
    public function getName () : string
66
    {
67 4
        return end($this->_name)['name'];
68
    }
69
70 2
    public function getHistory () : array
71
    {
72 2
        return $this->_name;
73
    }
74
75
    public function getCreateTimestamp () : float
76
    {
77
        return $this->_name[0]['timestamp'];
78
    }
79
80
    public function getTimestamp () : float
81
    {
82
        return end($this->_name)['timestamp'];
83
    }
84
85 4
    public function getProvisionalState () : bool
86
    {
87 4
        return $this->_provisional;
88
    }
89
90
        ///
91
92
    // INTERNAL
93
94 4
    private function checkName (string $name) : bool
95
    {
96 4
        foreach ($this->_link as &$link) :
97 2
            if (!$link->canAddCandidate($name)) :
98 2
                return false;
99
            endif;
100
        endforeach;
101
102 4
        return true;
103
    }
104
}
105