ElectionFactory::createBallotCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
use Psr\Log\LoggerInterface as Logger;
6
use Michaelc\Voting\Exception\VotingLogicException as LogicException;
7
use Michaelc\Voting\Exception\VotingRuntimeException as RuntimeException;
8
9
class ElectionFactory
10
{
11 6
	public static function createBallotCollection(array $rankings): array
12
	{
13 6
		$ballotCollection = [];
14
15 6
		foreach ($rankings as $ranking) {
16 6
			$ballotCollection[] = new Ballot($ranking);
17
		}
18
19 6
		return $ballotCollection;
20
	}
21
22 2
	public static function createCandidateCollection(array $candidates): array
23
	{
24 2
		$candidateCollection = [];
25
26 2
		$candidates = array_values($candidates);
27
28 2
		foreach ($candidates as $i => $candidate) {
29 2
			$candidateCollection[] = new Candidate($i);
30
		}
31
32 2
		return $candidateCollection;
33
	}
34
35 2
	public static function createCandidateBallotCollection(array $candidates, array $rankings): array
36
	{
37 2
		$candidateMatchup = $candidateCollection = [];
38
39 2
		$candidates = array_values($candidates);
40
41 2
		foreach ($candidates as $i => $name) {
42 2
			$candidateCollection[] = new Candidate($i);
43 2
			$candidateMatchup[$name] = $i;
44
		}
45
46 2
		foreach ($rankings as &$ranking) {
47 2
			array_walk($ranking, function (&$value, $key, $candidateMatchup) {
48 2
			    $value = $candidateMatchup[$value];
49 2
			}, $candidateMatchup);
50
		}
51
52 2
		$ballotCollection = self::createBallotCollection($rankings);
53
54 2
		return ['ballots' => $ballotCollection, 'candidates' => $candidateCollection];
55
	}
56
57 1
	public static function createElection(array $candidates, array $rankings, int $winnerCount, bool $ids = true): Election
58
	{
59 1
		if ($ids) {
60 1
			$candidates = self::createCandidateCollection($candidates);
61 1
			$ballots = self::createBallotCollection($rankings);
62
		} else {
63 1
			$collections = self::createCandidateBallotCollection($candidates, $rankings);
64 1
			$candidates = $collections['candidates'];
65 1
			$ballots = $collections['ballots'];
66
		}
67
68 1
		return new Election($candidates, $ballots, $winnerCount);
69
	}
70
}
71