Completed
Push — master ( b77c5c...4fbeac )
by Jeroen
10s
created

TaskNode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 6
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
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 Webcreate\Conveyor\Config\Definition;
13
14
use Symfony\Component\Config\Definition\ArrayNode;
15
use Symfony\Component\Config\Definition\ConfigurationInterface;
16
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17
use Symfony\Component\Config\Definition\NodeInterface;
18
use Webcreate\Conveyor\Factory\TaskFactory;
19
20 View Code Duplication
class TaskNode extends ArrayNode
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    protected $prepared = false;
23
    protected $originalChildren = array();
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param string        $name
29
     * @param NodeInterface $parent
30
     */
31 2
    public function __construct($name, NodeInterface $parent = null)
32
    {
33 2
        parent::__construct($name, $parent);
34
35
        //$this->setIgnoreExtraKeys(true);
36 2
    }
37
38
    /**
39
     * Set task factory
40
     *
41
     * @param TaskFactory|null $factory
42
     */
43 2
    public function setTaskFactory($factory)
44
    {
45 2
        $this->taskFactory = $factory;
0 ignored issues
show
Bug introduced by
The property taskFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46 2
    }
47
48
    /**
49
     * Takes child nodes from a ConfigurtionInterface instance
50
     * and adds these to this node
51
     *
52
     * @param  string                        $type
53
     * @throws InvalidConfigurationException
54
     */
55
    protected function prepareChildren($type)
56
    {
57
        // when we hit this function for the first time,
58
        // we store the original children. Each time we
59
        // hit this function we reset the children to the
60
        // original ones.
61
        if (false === $this->prepared) {
62
            $this->originalChildren = $this->children;
63
            $this->prepared = true;
64
        }
65
66
        $tasks = $this->taskFactory->getTasks();
67
68
        $this->children = $this->originalChildren;
69
70
        if (isset($tasks[$type])) {
71
            $configuration = $this->taskFactory->configuration($type);
72
73
            if ($configuration instanceof ConfigurationInterface) {
74
                $tree = $configuration->getConfigTreeBuilder()->buildTree();
75
                foreach ($tree->getChildren() as $child) {
76
                    $this->addChild($child);
77
                }
78
            }
79
        } else {
80
            throw new InvalidConfigurationException(sprintf(
81
                    'Task type "%s" does not exist at path "%s". Did you mean any of %s?', $type,
82
                    $this->getPath(),
83
                    implode(', ', array_keys($tasks))
84
            ));
85
        }
86
    }
87
88
    /**
89
     * We hook into the validateType method, this
90
     * gets called form the normalize method.
91
     *
92
     * @param mixed $value
93
     *
94
     * @throws InvalidTypeException
95
     */
96
    protected function validateType($value)
97
    {
98
        if (isset($value['type'])) {
99
            $this->prepareChildren($value['type']);
100
        } else {
101
            // ignore extra keys so the error message will be
102
            // focused on the missing type field
103
            $this->setIgnoreExtraKeys(true);
104
        }
105
106
        parent::validateType($value);
107
    }
108
}
109