Completed
Push — master ( 7d0f5c...bcb4e0 )
by Michael
02:55
created

Election::getActiveCandidateIds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
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 5
    public function __construct(int $winnersCount, array $candidates, array $ballots)
43
    {
44 5
        $this->winnersCount = $winnersCount;
45 5
        $this->candidates = $candidates;
46 5
        $this->ballots = $ballots;
47 5
        $this->candidateCount = count($candidates);
48 5
    }
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 3
    public function getCandidateCount(): int
68
    {
69 3
        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
    public function getActiveCandidateIds(): array
83
    {
84
        $ids = [];
85
86
        foreach ($this->getActiveCandidates() as $i => $candidate) {
87
            $ids[] = $candidate->getId();
88
        }
89
90
        return $ids;
91
    }
92
93
    /**
94
     * Get an array of candidates elected.
95
     *
96
     * @return \Michaelc\Voting\STV\Candidate[]
97
     */
98 1
    public function getElectedCandidates(): array
99
    {
100 1
        return $this->getStateCandidates(Candidate::ELECTED);
101
    }
102
103
    /**
104
     * Get an array of candidates defeated.
105
     *
106
     * @return \Michaelc\Voting\STV\Candidate[]
107
     */
108 1
    public function getDefeatedCandidates(): array
109
    {
110 1
        return $this->getStateCandidates(Candidate::DEFEATED);
111
    }
112
113
    /**
114
     * Get all candidates of a specific state.
115
     *
116
     * @param int $state A candidate state (See Candidate constants)
117
     *
118
     * @return \Michaelc\Voting\STV\Candidate[]
119
     */
120 2
    public function getStateCandidates(int $state): array
121
    {
122 2
        $candidates = [];
123
124 2
        foreach ($this->candidates as $candidateId => $candidate) {
125 2
            if ($candidate->getState() == $state) {
126 2
                $candidates[$candidateId] = $candidate;
127
            }
128
        }
129
130 2
        return $candidates;
131
    }
132
133
    /**
134
     * Get a count of candidates still running (not elected or defeated).
135
     *
136
     * @return int
137
     */
138 1
    public function getActiveCandidateCount(): int
139
    {
140 1
        return count($this->getActiveCandidates());
141
    }
142
143
    /**
144
     * Get an array of candidate ids.
145
     *
146
     * @return int[]
147
     */
148 2
    public function getCandidateIds(): array
149
    {
150 2
        $candidateIds = [];
151
152 2
        foreach ($this->candidates as $i => $candidate) {
153 2
            $candidateIds[] = $candidate->getId();
154
        }
155
156 2
        return $candidateIds;
157
    }
158
159
    /**
160
     * Gets the value of winnersCount.
161
     *
162
     * @return int
163
     */
164 1
    public function getWinnersCount(): int
165
    {
166 1
        return $this->winnersCount;
167
    }
168
169
    /**
170
     * Gets the value of candidates.
171
     *
172
     * @return array
173
     */
174 2
    public function getCandidates(): array
175
    {
176 2
        return $this->candidates;
177
    }
178
179
    /**
180
     * Gets the value of ballots.
181
     *
182
     * @return array
183
     */
184 2
    public function getBallots(): array
185
    {
186 2
        return $this->ballots;
187
    }
188
189
    /**
190
     * Get the number of ballots.
191
     *
192
     * @return int
193
     */
194 3
    public function getNumBallots(): int
195
    {
196 3
        return count($this->ballots);
197
    }
198
199
    /**
200
     * Get status of all candidates.
201
     *
202
     * @return array
203
     */
204
    public function getCandidatesStatus(): array
205
    {
206
        $candidates = [];
207
208
        foreach ($this->getElectedCandidates() as $i => $candidate) {
209
            $candidates['elected'][] = $candidate->getId();
210
        }
211
212
        foreach ($this->getActiveCandidates() as $i => $candidate) {
213
            $candidates['active'][] = [$candidate->getId(), $candidate->getVotes()];
214
        }
215
216
        foreach ($this->getDefeatedCandidates() as $i => $candidate) {
217
            $candidates['defeated'][] = $candidate->getId();
218
        }
219
220
        return $candidates;
221
    }
222
}
223