Datagen::load()   B
last analyzed

Complexity

Conditions 9
Paths 32

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9.2363

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 12
cts 14
cp 0.8571
rs 8.0555
c 0
b 0
f 0
cc 9
nc 32
nop 3
crap 9.2363
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
        if (!isset($fixture)) {
39
            return;
40
        }
41
42 2
        foreach ($this->processors as $processor) {
43 2
            $processorOptions = isset($options[$fixture->getProcessor()]) ? $options[$fixture->getProcessor()] : [];
44
45 2
            $processor->flush($processorOptions);
46
        }
47 2
    }
48
49 2
    private function getProcessor(string $name): ProcessorInterface
50
    {
51 2
        if (!\array_key_exists($name, $this->processors)) {
52
            throw new UnknownProcessorException($name);
53
        }
54
55 2
        return $this->processors[$name];
56
    }
57
}
58