SchemaConfiguration::getConfigTreeBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Generator\Schema\Configuration;
13
14
use Rocket\ORM\Model\Map\TableMap;
15
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
use Symfony\Component\Config\Definition\ConfigurationInterface;
19
20
/**
21
 * @author Sylvain Lorinet <[email protected]>
22
 */
23
class SchemaConfiguration implements ConfigurationInterface
24
{
25
    /**
26
     * Generates the configuration tree builder.
27
     *
28
     * @return TreeBuilder The tree builder
29
     */
30 1
    public function getConfigTreeBuilder()
31
    {
32 1
        $treeBuilder = new TreeBuilder();
33 1
        $rootNode = $treeBuilder->root('schema');
34
35 1
        $this->addSchemaConfigurationNode($rootNode);
36
37 1
        return $treeBuilder;
38
    }
39
40
    /**
41
     * @param ArrayNodeDefinition $node
42
     */
43 1
    protected function addSchemaConfigurationNode(ArrayNodeDefinition $node)
44
    {
45
        $node
46 1
            ->fixXmlConfig('table')
47 1
            ->children()
48 1
                ->scalarNode('connection')
49 1
                    ->defaultValue('default')
50 1
                ->end()
51 1
                ->scalarNode('database')
52 1
                    ->isRequired()
53 1
                    ->cannotBeEmpty()
54 1
                ->end()
55 1
                ->scalarNode('directory')
56 1
                    ->defaultValue('/../Model')
57 1
                ->end()
58 1
                ->scalarNode('namespace')
59 1
                    ->isRequired()
60 1
                    ->cannotBeEmpty()
61 1
                ->end()
62
63 1
                ->append($this->getTableConfigurationNode())
64 1
            ->end()
65
        ;
66 1
    }
67
68
    /**
69
     * @return ArrayNodeDefinition|NodeDefinition
70
     */
71 1
    protected function getTableConfigurationNode()
72
    {
73 1
        $builder = new TreeBuilder();
74 1
        $tablesNode = $builder->root('tables');
75
        $tablesNode
0 ignored issues
show
Bug introduced by
The method useAttributeAsKey() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. Did you maybe mean attribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
76 1
            ->requiresAtLeastOneElement()
77 1
            ->isRequired()
78 1
            ->useAttributeAsKey('name')
79 1
            ->prototype('array')
80 1
            ->fixXmlConfig('column')
81 1
            ->fixXmlConfig('relation')
82
83 1
            ->children()
84 1
                ->scalarNode('phpName')
85 1
                    ->defaultNull()
86 1
                ->end()
87 1
            ->end()
88 1
            ->children()
89 1
                ->scalarNode('type')
90 1
                    ->defaultValue('InnoDB')
91 1
                ->end()
92 1
            ->end()
93
94 1
            ->append($this->getTableColumnConfigurationNode())
95 1
            ->append($this->getTableRelationConfigurationNode())
96
        ;
97
98 1
        return $tablesNode;
99
    }
100
101
    /**
102
     * @return ArrayNodeDefinition|NodeDefinition
103
     */
104 1
    protected function getTableColumnConfigurationNode()
105
    {
106 1
        $builder = new TreeBuilder();
107 1
        $columnsNode = $builder->root('columns');
108
        $columnsNode
0 ignored issues
show
Bug introduced by
The method useAttributeAsKey() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. Did you maybe mean attribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
109 1
            ->requiresAtLeastOneElement()
110 1
            ->isRequired()
111 1
            ->useAttributeAsKey('name')
112 1
            ->prototype('array')
113 1
                ->fixXmlConfig('value')
114 1
                ->children()
115 1
                    ->scalarNode('phpName')
116 1
                        ->defaultNull()
117 1
                    ->end()
118 1
                    ->scalarNode('type')
119 1
                        ->isRequired()
120 1
                        ->cannotBeEmpty()
121 1
                        ->beforeNormalization()
122 1
                        ->ifString()
123 1
                            ->then(function ($value) {
124
                                return TableMap::convertColumnTypeToConstant($value); // @codeCoverageIgnore
125 1
                            })
126 1
                        ->end()
127 1
                        ->validate()
128 1
                            ->ifNotInArray(range(1, 9))
129 1
                            ->thenInvalid('Invalid column type for value "%s"')
130 1
                        ->end()
131 1
                    ->end()
132 1
                    ->integerNode('size')
133 1
                        ->min(1)
134 1
                        ->max(255)
135 1
                        ->defaultNull()
136 1
                    ->end()
137
                    // TODO unsigned
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
138 1
                    ->integerNode('decimal')
139 1
                        ->min(1)
140 1
                        ->max(20)
141 1
                        ->defaultNull()
142 1
                    ->end()
143 1
                    ->scalarNode('default')
144 1
                        ->defaultNull()
145 1
                    ->end()
146 1
                    ->arrayNode('values')
147 1
                        ->prototype('scalar')->end()
148 1
                    ->end()
149 1
                    ->booleanNode('required')
150 1
                        ->defaultFalse()
151 1
                    ->end()
152 1
                    ->booleanNode('primaryKey')
153 1
                        ->defaultFalse()
154 1
                    ->end()
155 1
                    ->booleanNode('autoIncrement')
156 1
                        ->defaultFalse()
157 1
                    ->end()
158 1
                    ->scalarNode('description')
159 1
                        ->defaultNull()
160 1
                    ->end()
161 1
                ->end()
162 1
            ->end()
163
        ;
164
165 1
        return $columnsNode;
166
    }
167
168
    /**
169
     * @return ArrayNodeDefinition|NodeDefinition
170
     */
171 1
    protected function getTableRelationConfigurationNode()
172
    {
173 1
        $onActionBehaviors = ['CASCADE', 'NO ACTION', 'RESTRICT', 'SET NULL', 'SET DEFAULT'];
174
175 1
        $builder = new TreeBuilder();
176 1
        $relationsNode = $builder->root('relations');
177
        $relationsNode
178 1
            ->useAttributeAsKey('with')
179 1
            ->prototype('array')
180 1
                ->children()
181 1
                    ->scalarNode('name')
182 1
                        ->defaultNull()
183 1
                    ->end()
184 1
                    ->scalarNode('phpName')
185 1
                        ->defaultNull()
186 1
                    ->end()
187 1
                    ->scalarNode('local')
188 1
                        ->defaultNull()
189 1
                    ->end()
190 1
                    ->scalarNode('foreign')
191 1
                        ->defaultNull()
192 1
                    ->end()
193 1
                    ->scalarNode('onUpdate')
194 1
                        ->defaultValue('RESTRICT')
195 1
                        ->validate()
196 1
                            ->ifNotInArray($onActionBehaviors)
197 1
                            ->thenInvalid('Invalid "onUpdate" behavior for value "%s", available : ' . join(', ', $onActionBehaviors))
198 1
                        ->end()
199 1
                    ->end()
200 1
                    ->scalarNode('onDelete')
201 1
                        ->defaultValue('RESTRICT')
202 1
                        ->validate()
203 1
                            ->ifNotInArray($onActionBehaviors)
204 1
                            ->thenInvalid('Invalid "onDelete" behavior for value "%s", available : ' . join(', ', $onActionBehaviors))
205 1
                        ->end()
206 1
                    ->end()
207 1
                ->end()
208 1
            ->end()
209
        ;
210
211 1
        return $relationsNode;
212
    }
213
}
214