Completed
Push — master ( bfc6cf...82a6c1 )
by Michael
03:57
created

Election::getCandidatesStatus()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 9
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 8
nop 0
crap 20
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
class Election
6
{
7
    /**
8
     * Count of candidates in election.
9
     *
10
     * @var int
11
     */
12
    protected $candidateCount;
13
14
    /**
15
     * Count of number of seats/winners.
16
     *
17
     * @var int
18
     */
19
    protected $winnersCount;
20
21
    /**
22
     * Array of candidates competing in election.
23
     *
24
     * @var array
25
     */
26
    protected $candidates;
27
28
    /**
29
     * Array of ballots cast in election.
30
     *
31
     * @var array
32
     */
33
    protected $ballots;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param int   $winnersCount Number of winners to allocate
39
     * @param array $candidates   Array of candidates competing
40
     * @param array $ballots      Array of all ballots cast in election
41
     */
42 4
    public function __construct(int $winnersCount, array $candidates, array $ballots)
43
    {
44 4
        $this->winnersCount = $winnersCount;
45 4
        $this->candidates = $candidates;
46 4
        $this->ballots = $ballots;
47 4
        $this->candidateCount = count($candidates);
48 4
    }
49
50
    /**
51
     * Get a specific candidate object by their ID.
52
     *
53
     * @param int $id ID of the candidate to get
54
     *
55
     * @return \Michaelc\Voting\STV\Candidate
56
     */
57 1
    public function getCandidate(int $id): Candidate
58
    {
59 1
        return $this->candidates[$id];
60
    }
61
62
    /**
63
     * Get a count of candidates competing.
64
     *
65
     * @return int
66
     */
67 2
    public function getCandidateCount(): int
68
    {
69 2
        return $this->candidateCount;
70
    }
71
72
    /**
73
     * Get an array of candidates still running (not elected or defeated).
74
     *
75
     * @return \Michaelc\Voting\STV\Candidate[]
76
     */
77 2
    public function getActiveCandidates(): array
78
    {
79 2
        return $this->getStateCandidates(Candidate::RUNNING);
80
    }
81
82
    /**
83
     * Get an array of candidates elected.
84
     *
85
     * @return \Michaelc\Voting\STV\Candidate[]
86
     */
87 1
    public function getElectedCandidates(): array
88
    {
89 1
        return $this->getStateCandidates(Candidate::ELECTED);
90
    }
91
92
    /**
93
     * Get an array of candidates defeated.
94
     *
95
     * @return \Michaelc\Voting\STV\Candidate[]
96
     */
97 1
    public function getDefeatedCandidates(): array
98
    {
99 1
        return $this->getStateCandidates(Candidate::DEFEATED);
100
    }
101
102
    /**
103
     * Get all candidates of a specific state.
104
     *
105
     * @param int $state A candidate state (See Candidate constants)
106
     *
107
     * @return \Michaelc\Voting\STV\Candidate[]
108
     */
109 2
    public function getStateCandidates(int $state): array
110
    {
111 2
        $candidates = [];
112
113 2
        foreach ($this->candidates as $candidateId => $candidate) {
114 2
            if ($candidate->getState() == $state) {
115 2
                $candidates[$candidateId] = $candidate;
116
            }
117
        }
118
119 2
        return $candidates;
120
    }
121
122
    /**
123
     * Get a count of candidates still running (not elected or defeated).
124
     *
125
     * @return int
126
     */
127 1
    public function getActiveCandidateCount(): int
128
    {
129 1
        return count($this->getActiveCandidates());
130
    }
131
132
    /**
133
     * Get an array of candidate ids.
134
     *
135
     * @return int[]
136
     */
137 1
    public function getCandidateIds(): array
138
    {
139 1
        $candidateIds = [];
140
141 1
        foreach ($this->candidates as $i => $candidate) {
142 1
            $candidateIds[] = $candidate->getId();
143
        }
144
145 1
        return $candidateIds;
146
    }
147
148
    /**
149
     * Gets the value of winnersCount.
150
     *
151
     * @return int
152
     */
153 1
    public function getWinnersCount(): int
154
    {
155 1
        return $this->winnersCount;
156
    }
157
158
    /**
159
     * Gets the value of candidates.
160
     *
161
     * @return array
162
     */
163 2
    public function getCandidates(): array
164
    {
165 2
        return $this->candidates;
166
    }
167
168
    /**
169
     * Gets the value of ballots.
170
     *
171
     * @return array
172
     */
173 1
    public function getBallots(): array
174
    {
175 1
        return $this->ballots;
176
    }
177
178
    /**
179
     * Get the number of ballots.
180
     *
181
     * @return int
182
     */
183 1
    public function getNumBallots(): int
184
    {
185 1
        return count($this->ballots);
186
    }
187
188
    /**
189
     * Get status of all candidates.
190
     *
191
     * @return array
192
     */
193
    public function getCandidatesStatus(): array
194
    {
195
        $candidates = [];
196
197
        foreach ($this->getElectedCandidates() as $i => $candidate) {
198
            $candidates['elected'][] = $candidate->getId();
199
        }
200
201
        foreach ($this->getActiveCandidates() as $i => $candidate) {
202
            $candidates['active'][] = [$candidate->getId(), $candidate->getVotes()];
203
        }
204
205
        foreach ($this->getDefeatedCandidates() as $i => $candidate) {
206
            $candidates['defeated'][] = $candidate->getId();
207
        }
208
209
        return $candidates;
210
    }
211
}
212