Completed
Push — master ( b28f87...a255d6 )
by Olivier
03:32
created

Loader::getFixtures()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 19
cts 19
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 18
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Datagen;
6
7
class Loader
8
{
9
    private $fixtureSorter;
10
11
    private $fixtures = [];
12
    private $groups = [];
13
14 7
    public function __construct(FixtureSorter $fixtureSorter = null)
15
    {
16 7
        $this->fixtureSorter = $fixtureSorter ?? new FixtureSorter();
17 7
    }
18
19 7
    public function addFixture(FixtureInterface $fixture, array $groups = []): void
20
    {
21 7
        $key = $this->getUniqueKey($fixture);
22
23 7
        $this->fixtures[$key] = $fixture;
24
25 7
        foreach ($groups as $group) {
26 5
            if (!isset($this->groups[$group])) {
27 5
                $this->groups[$group] = [];
28
            }
29 5
            if (!in_array($key, $this->groups[$group])) {
30 5
                $this->groups[$group][] = $key;
31
            }
32
        }
33 7
    }
34
35
    public function getFixturesByGroups(): array
36
    {
37
        $groups = [];
38
39
        foreach ($this->groups as $name => $fixtures) {
40
            $groupFixtures = [];
41
42
            foreach ($fixtures as $fixtureUniqueKey) {
43
                $groupFixtures[] = $this->fixtures[$fixtureUniqueKey];
44
            }
45
46
            $groups[$name] = $this->fixtureSorter->sort($groupFixtures);
47
        }
48
49
        return $groups;
50
    }
51
52 7
    public function getFixtures(array $groups = [], array $excludeGroups = []): array
53
    {
54 7
        $duplicatedGroups = array_intersect($groups, $excludeGroups);
55 7
        if (0 < count($duplicatedGroups)) {
56 1
            throw new \InvalidArgumentException(sprintf('You can\'t both select & ignore a given group. Errored: ["%s"]', implode('", "', $duplicatedGroups)));
57
        }
58
59
        // Check that all groups exists.
60 6
        foreach ($groups + $excludeGroups as $group) {
61 3
            if (!isset($this->groups[$group])) {
62 1
                throw new \InvalidArgumentException(sprintf('Unknown group "%s". Available: ["%s"]', $group, implode('", "', array_keys($this->groups))));
63
            }
64
        }
65
66
        // Select all relevant fixtures according to asked groups
67 5
        if (0 === count($groups)) {
68 4
            $fixtures = $this->fixtures;
69
        } else {
70 1
            $fixtures = [];
71 1
            foreach ($groups as $group) {
72 1
                foreach ($this->groups[$group] as $fixtureInGroup) {
73 1
                    $fixtures[$fixtureInGroup] = $this->fixtures[$fixtureInGroup];
74
                }
75
            }
76
        }
77
78
        // Remove all fixtures to exclude
79 5
        foreach ($excludeGroups as $excludeGroup) {
80 1
            foreach ($this->groups[$excludeGroup] as $fixtureToExclude) {
81 1
                if (array_key_exists($fixtureToExclude, $fixtures)) {
82 1
                    unset($fixtures[$fixtureToExclude]);
83
                }
84
            }
85
        }
86
87 5
        $fixtures = $this->fixtureSorter->sort($fixtures);
88
89 5
        return $fixtures;
90
    }
91
92 7
    private function getUniqueKey(FixtureInterface $fixture): string
93
    {
94 7
        return $fixture->getProcessor().'-'.$fixture->getName();
95
    }
96
}
97