Completed
Push — master ( 9d6231...f3ed4a )
by Olivier
09:25
created

Datagen::load()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.048

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 11
cp 0.9091
rs 8.4444
c 0
b 0
f 0
cc 8
nc 16
nop 3
crap 8.048
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Datagen;
6
7
use Shapin\Datagen\Exception\UnknownProcessorException;
8
9
class Datagen
10
{
11
    private $loader;
12
    private $processors = [];
13
14 2
    public function __construct(Loader $loader, iterable $processors)
15
    {
16 2
        $this->loader = $loader;
17 2
        foreach ($processors as $processor) {
18 2
            $this->processors[$processor->getName()] = $processor;
19
        }
20 2
    }
21
22 2
    public function load(array $groups = [], array $excludeGroups = [], array $options = []): void
23
    {
24 2
        $fixtures = $this->loader->getFixtures($groups, $excludeGroups);
25 2
        $wantedProcessor = isset($options['processor']) ? $options['processor'] : null;
26
27 2
        foreach ($fixtures as $fixture) {
28
            // Ignore this fixture if it doesn't depends on the wanted processor.
29 2
            if (null !== $wantedProcessor && $wantedProcessor !== $fixture->getProcessor()) {
30
                continue;
31
            }
32
33 2
            $processorOptions = isset($options[$fixture->getProcessor()]) ? $options[$fixture->getProcessor()] : [];
34
35 2
            $this->getProcessor($fixture->getProcessor())->process($fixture, $processorOptions);
36
        }
37
38 2
        foreach ($this->processors as $processor) {
39 2
            $processor->flush($options[$fixture->getProcessor()] ?: []);
0 ignored issues
show
Bug introduced by
The variable $fixture seems to be defined by a foreach iteration on line 27. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
40
        }
41 2
    }
42
43 2
    private function getProcessor(string $name): ProcessorInterface
44
    {
45 2
        if (!array_key_exists($name, $this->processors)) {
46
            throw new UnknownProcessorException($name);
47
        }
48
49 2
        return $this->processors[$name];
50
    }
51
}
52