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

Datagen   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 43
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
B load() 0 20 8
A getProcessor() 0 8 2
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