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

GeneratorManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 73
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A registerGenerator() 0 4 1
D generate() 0 45 10
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