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

Datagen   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 37
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A load() 0 14 5
A getProcessor() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Datagen;
6
7
use Shapin\Exception\UnknwonProcessorException;
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
26 2
        foreach ($fixtures as $fixture) {
27 2
            $processorOptions = $options[$fixture->getProcessor()] ?: [];
28
29 2
            $this->getProcessor($fixture->getProcessor())->process($fixture, $processorOptions);
30
        }
31
32 2
        foreach ($this->processors as $processor) {
33 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 26. 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...
34
        }
35 2
    }
36
37 2
    private function getProcessor(string $name): ProcessorInterface
38
    {
39 2
        if (!array_key_exists($name, $this->processors)) {
40
            throw new UnknwonProcessorException($name);
41
        }
42
43 2
        return $this->processors[$name];
44
    }
45
}
46