WithScoresModifier::processObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace TournamentGenerator\Export\Modifiers;
5
6
7
use Exception;
8
use InvalidArgumentException;
9
use TournamentGenerator\Team;
10
11
class WithScoresModifier implements Modifier
12
{
13
14
	/**
15
	 * @inheritDoc
16
	 * @throws Exception
17
	 */
18 17
	public static function process(array &$data) : array {
19
		// Check for "single" export
20 17
		if (isset($data['object'])) {
21 9
			if (!$data['object'] instanceof Team) {
22 1
				throw new InvalidArgumentException('WithScores modifier needs a Team object.');
23
			}
24 8
			return self::processArray($data);
25
		}
26
27 8
		foreach ($data as $object) {
28 8
			if (!isset($object->object) || !$object->object instanceof Team) {
29 3
				throw new InvalidArgumentException('WithScores modifier needs a Team object.');
30
			}
31 5
			self::processObject($object);
32
		}
33 5
		return $data;
34
	}
35
36
	/**
37
	 * Modify a "Single" data
38
	 *
39
	 * @param array $data
40
	 *
41
	 * @return array
42
	 * @throws Exception
43
	 */
44 8
	protected static function processArray(array &$data) : array {
45
		/** @var Team $team */
46 8
		$team = $data['object'];
47 8
		$data['scores'] = array_map(static function(array $group) {
48 6
			unset($group['group']); // Get rid of the Group object reference
49 6
			return $group;
50 8
		}, $team->getGroupResults());
51 8
		return $data;
52
	}
53
54
	/**
55
	 * Modify data
56
	 *
57
	 * @param object $object
58
	 *
59
	 * @return object
60
	 * @throws Exception
61
	 */
62 5
	protected static function processObject(object $object) : object {
63
		/** @var Team $team */
64 5
		$team = $object->object;
65 5
		$object->scores = array_map(static function(array $group) {
66 4
			unset($group['group']); // Get rid of the Group object reference
67 4
			return $group;
68 5
		}, $team->getGroupResults());
69 5
		return $object;
70
	}
71
}