ElectionFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 62
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createBallotCollection() 0 10 2
A createCandidateCollection() 0 12 2
A createCandidateBallotCollection() 0 21 3
A createElection() 0 13 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