Completed
Pull Request — master (#10)
by Tomáš
04:50 queued 01:55
created

GeneratorManager::generate()   D

Complexity

Conditions 10
Paths 36

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 0
cts 38
cp 0
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 26
nc 36
nop 2
crap 110

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
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Generator;
13
14
use Dflydev\DotAccessConfiguration\Configuration;
15
use Symplify\PHP7_Sculpin\Source\SourceInterface;
16
use Symplify\PHP7_Sculpin\Source\SourceSet;
17
18
final class GeneratorManager
19
{
20
    /**
21
     * @var Configuration
22
     */
23
    private $siteConfiguration;
24
25
    /**
26
     * @var GeneratorInterface[]
27
     */
28
    private $generators = [];
29
30
    public function __construct(Configuration $siteConfiguration)
31
    {
32
        $this->siteConfiguration = $siteConfiguration;
33
    }
34
35
    public function registerGenerator(string $name, GeneratorInterface $generator)
36
    {
37
        $this->generators[$name] = $generator;
38
    }
39
40
    /**
41
     * @throws \InvalidArgumentException
42
     *
43
     * @return string
44
     */
45
    public function generate(SourceInterface $source, SourceSet $sourceSet)
46
    {
47
        $data = $source->data();
48
49
        $generators = [];
50
        $isGenerator = $source->isGenerator();
51
        if ($generatorNames = $data->get('generator')) {
52
            if (!$isGenerator) {
53
                $source->setIsGenerator();
54
            }
55
56
            foreach ((array) $generatorNames as $generatorName) {
57
                if (!isset($this->generators[$generatorName])) {
58
                    throw new \InvalidArgumentException(
59
                        "Requested generator '$generatorName' could not be found; was it registered?"
60
                    );
61
                }
62
63
                $generators[] = $this->generators[$generatorName];
64
            }
65
        } else {
66
            if ($isGenerator) {
67
                $source->setIsNotGenerator();
68
            }
69
70
            return;
71
        }
72
73
        $targetSources = [$source];
74
75
        foreach ($generators as $generator) {
76
            $newTargetSources = [];
77
            foreach ($targetSources as $targetSource) {
78
                foreach ((array) $generator->generate($targetSource) as $generatedSource) {
79
                    $generatedSource->setIsGenerated();
80
                    $newTargetSources[] = $generatedSource;
81
                }
82
            }
83
            $targetSources = $newTargetSources;
84
        }
85
86
        foreach ($targetSources as $generatedSource) {
87
            $sourceSet->mergeSource($generatedSource);
88
        }
89
    }
90
}
91