Completed
Push — master ( da2f6e...d3b8cb )
by Kamil
20:06
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4285
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\CoreBundle\DependencyInjection;
13
14
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
15
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
16
use Sylius\Component\Core\Model\ProductVariantImage;
17
use Sylius\Component\Core\Model\ProductVariantImageInterface;
18
use Sylius\Component\Resource\Factory\Factory;
19
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
21
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
22
use Symfony\Component\Config\Definition\ConfigurationInterface;
23
24
class Configuration implements ConfigurationInterface
25
{
26
    public function getConfigTreeBuilder()
27
    {
28
        $treeBuilder = new TreeBuilder();
29
        $rootNode = $treeBuilder->root('sylius_core');
30
31
        $rootNode
32
            ->addDefaultsIfNotSet()
33
            ->children()
34
                ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
35
                ->scalarNode('currency_storage')->defaultValue('sylius.storage.session')->end()
36
            ->end()
37
        ;
38
39
        $this->addResourcesSection($rootNode);
40
        $this->addRoutingSection($rootNode);
41
        $this->addCheckoutSection($rootNode);
42
        $this->addSitemapSection($rootNode);
43
44
        return $treeBuilder;
45
    }
46
47
    /**
48
     * @param ArrayNodeDefinition $node
49
     */
50
    private function addResourcesSection(ArrayNodeDefinition $node)
51
    {
52
        $node
53
            ->children()
54
                ->arrayNode('resources')
55
                    ->addDefaultsIfNotSet()
56
                    ->children()
57
                        ->arrayNode('product_variant_image')
58
                            ->addDefaultsIfNotSet()
59
                            ->children()
60
                                ->variableNode('options')->end()
61
                                ->arrayNode('classes')
62
                                    ->addDefaultsIfNotSet()
63
                                    ->children()
64
                                        ->scalarNode('model')->defaultValue(ProductVariantImage::class)->cannotBeEmpty()->end()
65
                                        ->scalarNode('interface')->defaultValue(ProductVariantImageInterface::class)->cannotBeEmpty()->end()
66
                                        ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
67
                                        ->scalarNode('factory')->defaultValue(Factory::class)->end()
68
                                    ->end()
69
                                ->end()
70
                            ->end()
71
                        ->end()
72
                    ->end()
73
                ->end()
74
            ->end()
75
        ;
76
    }
77
78
    /**
79
     * @param ArrayNodeDefinition $node
80
     */
81
    private function addRoutingSection(ArrayNodeDefinition $node)
82
    {
83
        $node
84
            ->children()
85
                ->scalarNode('route_collection_limit')->defaultValue(0)->info('Limit the number of routes that are fetched when getting a collection, set to false to disable the limit.')->end()
86
                ->scalarNode('route_uri_filter_regexp')->defaultValue('')->info('Regular expression filter which is used to skip the Sylius dynamic router for any request URI that matches.')->end()
87
                ->arrayNode('routing')->isRequired()->cannotBeEmpty()
88
                    ->info('Classes for which routes should be generated.')
89
                    ->useAttributeAsKey('class_name')
90
                    ->prototype('array')
91
                    ->children()
92
                        ->scalarNode('field')->isRequired()->cannotBeEmpty()->info('Field representing the URI path.')->end()
93
                        ->scalarNode('prefix')->defaultValue('')->info('Prefix applied to all routes.')->end()
94
                        ->arrayNode('defaults')->isRequired()->cannotBeEmpty()->info('Defaults to add to the generated route.')
95
                            ->children()
96
                                ->scalarNode('controller')->isRequired()->cannotBeEmpty()->info('Controller where the request should be routed.')->end()
97
                                ->scalarNode('repository')->isRequired()->cannotBeEmpty()->info('Repository where the router will find the class.')->end()
98
                                ->arrayNode('sylius')->isRequired()->cannotBeEmpty()->info('Sylius defaults to add to generated route.')
99
                                    ->useAttributeAsKey('sylius')
100
                                    ->prototype('variable')
101
                                ->end()
102
                            ->end()
103
                        ->end()
104
                    ->end()
105
                ->end()
106
            ->end()
107
        ;
108
    }
109
110
    /**
111
     * @param ArrayNodeDefinition $node
112
     */
113
    private function addCheckoutSection(ArrayNodeDefinition $node)
114
    {
115
        $node
116
            ->children()
117
                ->arrayNode('checkout')
118
                    ->addDefaultsIfNotSet()
119
                    ->children()
120
                        ->arrayNode('steps')
121
                            ->addDefaultsIfNotSet()
122
                            ->info('Templates used for steps in the checkout flow process')
123
                            ->children()
124
                                ->append($this->addCheckoutStepNode('security', 'SyliusWebBundle:Frontend/Checkout/Step:security.html.twig'))
125
                                ->append($this->addCheckoutStepNode('addressing', 'SyliusWebBundle:Frontend/Checkout/Step:addressing.html.twig'))
126
                                ->append($this->addCheckoutStepNode('shipping', 'SyliusWebBundle:Frontend/Checkout/Step:shipping.html.twig'))
127
                                ->append($this->addCheckoutStepNode('payment', 'SyliusWebBundle:Frontend/Checkout/Step:payment.html.twig'))
128
                                ->append($this->addCheckoutStepNode('finalize', 'SyliusWebBundle:Frontend/Checkout/Step:finalize.html.twig'))
129
                            ->end()
130
                        ->end()
131
                    ->end()
132
                ->end()
133
            ->end()
134
        ;
135
    }
136
137
    /**
138
     * @param ArrayNodeDefinition $node
139
     */
140
    private function addSitemapSection(ArrayNodeDefinition $node)
141
    {
142
        $node
143
            ->children()
144
                ->arrayNode('sitemap')
145
                    ->children()
146
                        ->scalarNode('template')->defaultValue('@SyliusCore/Sitemap/show.xml.twig')->end()
147
                    ->end()
148
                ->end()
149
            ->end()
150
        ;
151
    }
152
153
    /**
154
     * Helper method to append checkout step nodes.
155
     *
156
     * @param $name
157
     * @param $defaultTemplate
158
     *
159
     * @return NodeDefinition
160
     */
161
    private function addCheckoutStepNode($name, $defaultTemplate)
162
    {
163
        $builder = new TreeBuilder();
164
        $node = $builder->root($name);
165
166
        $node
167
            ->addDefaultsIfNotSet()
168
            ->children()
169
                ->scalarNode('template')->defaultValue($defaultTemplate)->end()
170
            ->end()
171
        ;
172
173
        return $node;
174
    }
175
}
176