Completed
Push — 2.x-dev-kit ( 8d77e1 )
by
unknown
28:22 queued 25:50
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 149
Code Lines 135

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
c 7
b 0
f 1
dl 0
loc 149
rs 8.2857
cc 1
eloc 135
nc 1
nop 0

How to fix   Long Method   

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 part of the Sonata project.
5
 *
6
 * (c) Thomas Rabaix <[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 Sonata\NotificationBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
19
/**
20
 * This is the class that validates and merges configuration from your app/config files.
21
 *
22
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
23
 */
24
class Configuration implements ConfigurationInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getConfigTreeBuilder()
30
    {
31
        $treeBuilder = new TreeBuilder();
32
        $rootNode = $treeBuilder->root('sonata_notification')->children();
33
34
        $backendInfo = <<<EOF
35
Other backends you can use:
36
37
sonata.notification.backend.postpone
38
sonata.notification.backend.doctrine
39
sonata.notification.backend.rabbitmq
40
EOF;
41
42
        $iterationListenersInfo = <<<EOF
43
Listeners attached to the IterateEvent
44
Iterate event is thrown on each command iteration
45
46
Iteration listener class must implement Sonata\NotificationBundle\Event\IterationListener
47
EOF;
48
49
        $rootNode
50
            ->scalarNode('backend')
51
                ->info($backendInfo)
52
                ->defaultValue('sonata.notification.backend.runtime')
53
            ->end()
54
            ->append($this->getQueueNode())
55
            ->arrayNode('backends')
56
                ->children()
57
                    ->arrayNode('doctrine')
58
                        ->children()
59
                            ->scalarNode('message_manager')
60
                                ->defaultValue('sonata.notification.manager.message.default')
61
                            ->end()
62
                            ->scalarNode('max_age')
63
                                ->info('The max age in seconds')
64
                                ->defaultValue(86400)
65
                            ->end()
66
                            ->scalarNode('pause')
67
                                ->info('The delay in microseconds')
68
                                ->defaultValue(500000)
69
                            ->end()
70
                            ->scalarNode('batch_size')
71
                                ->info('The number of items on each iteration')
72
                                ->defaultValue(10)
73
                            ->end()
74
                            ->arrayNode('states')
75
                                ->info('Raising errors level')
76
                                ->addDefaultsIfNotSet()
77
                                ->children()
78
                                    ->integerNode('in_progress')
79
                                        ->defaultValue(10)
80
                                    ->end()
81
                                    ->integerNode('error')
82
                                        ->defaultValue(20)
83
                                    ->end()
84
                                    ->integerNode('open')
85
                                        ->defaultValue(100)
86
                                    ->end()
87
                                    ->integerNode('done')
88
                                        ->defaultValue(10000)
89
                                    ->end()
90
                                ->end()
91
                            ->end()
92
                        ->end()
93
                    ->end()
94
                    ->arrayNode('rabbitmq')
95
                        ->children()
96
                            ->scalarNode('exchange')
97
                                ->cannotBeEmpty()
98
                                ->isRequired()
99
                            ->end()
100
                            ->arrayNode('connection')
101
                                ->addDefaultsIfNotSet()
102
                                ->children()
103
                                    ->scalarNode('host')
104
                                        ->defaultValue('localhost')
105
                                    ->end()
106
                                    ->scalarNode('port')
107
                                        ->defaultValue(5672)
108
                                    ->end()
109
                                    ->scalarNode('user')
110
                                        ->defaultValue('guest')
111
                                    ->end()
112
                                    ->scalarNode('pass')
113
                                        ->defaultValue('guest')
114
                                    ->end()
115
                                    ->scalarNode('vhost')
116
                                        ->defaultValue('guest')
117
                                    ->end()
118
                                    ->scalarNode('console_url')
119
                                        ->defaultValue('http://localhost:55672/api')
120
                                    ->end()
121
                                ->end()
122
                            ->end()
123
                        ->end()
124
                    ->end()
125
                ->end()
126
            ->end()
127
            ->arrayNode('consumers')
128
                ->addDefaultsIfNotSet()
129
                ->children()
130
                    ->booleanNode('register_default')
131
                        ->info('If set to true, SwiftMailerConsumer and LoggerConsumer will be registered as services')
132
                        ->defaultTrue()
133
                    ->end()
134
                ->end()
135
            ->end()
136
            ->arrayNode('iteration_listeners')
137
                ->info($iterationListenersInfo)
138
                ->defaultValue(array())
139
                ->prototype('scalar')->end()
140
            ->end()
141
            ->arrayNode('class')
142
                ->addDefaultsIfNotSet()
143
                ->children()
144
                    ->scalarNode('message')
145
                        ->defaultValue('Application\\Sonata\\NotificationBundle\\Entity\\Message')
146
                    ->end()
147
                ->end()
148
            ->end()
149
            ->arrayNode('admin')
150
                ->addDefaultsIfNotSet()
151
                ->children()
152
                    ->booleanNode('enabled')
153
                        ->defaultTrue()
154
                    ->end()
155
                    ->arrayNode('message')
156
                        ->addDefaultsIfNotSet()
157
                        ->children()
158
                            ->scalarNode('class')
159
                                ->cannotBeEmpty()
160
                                ->defaultValue('Sonata\\NotificationBundle\\Admin\\MessageAdmin')
161
                            ->end()
162
                            ->scalarNode('controller')
163
                                ->cannotBeEmpty()
164
                                ->defaultValue('SonataNotificationBundle:MessageAdmin')
165
                            ->end()
166
                            ->scalarNode('translation')
167
                                ->cannotBeEmpty()
168
                                ->defaultValue('SonataNotificationBundle')
169
                            ->end()
170
                        ->end()
171
                    ->end()
172
                ->end()
173
            ->end()
174
        ;
175
176
        return $treeBuilder;
177
    }
178
179
    /**
180
     * @return ArrayNodeDefinition|NodeDefinition
181
     */
182
    protected function getQueueNode()
183
    {
184
        $treeBuilder = new TreeBuilder();
185
        $node = $treeBuilder->root('queues');
186
187
        $queuesInfo = <<<EOF
188
Example for using RabbitMQ
189
    - { queue: myQueue, recover: true, default: false, routing_key: the_routing_key, dead_letter_exchange: 'my.dead.letter.exchange' }
190
    - { queue: catchall, default: true }
191
192
Example for using Doctrine
193
    - { queue: sonata_page, types: [sonata.page.create_snapshot, sonata.page.create_snapshots] }
194
    - { queue: catchall, default: true }
195
EOF;
196
197
        $routingKeyInfo = <<<EOF
198
Only used by RabbitMQ
199
200
Direct exchange with routing_key
201
EOF;
202
203
        $recoverInfo = <<<EOF
204
Only used by RabbitMQ
205
206
If set to true, the consumer will respond with a `basic.recover` when an exception occurs,
207
otherwise it will not respond at all and the message will be unacknowledged
208
EOF;
209
210
        $deadLetterExchangeInfo = <<<EOF
211
Only used by RabbitMQ
212
213
If is set, failed messages will be rejected and sent to this exchange
214
EOF;
215
216
        $typesInfo = <<<EOF
217
Only used by Doctrine
218
219
Defines types handled by the message backend
220
EOF;
221
222
        $connectionNode = $node
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method requiresAtLeastOneElement() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
223
            ->info($queuesInfo)
224
            ->requiresAtLeastOneElement()
225
            ->prototype('array')
226
        ;
227
228
        $connectionNode
229
            ->children()
230
                ->scalarNode('queue')
231
                    ->info('The name of the queue')
232
                    ->cannotBeEmpty()
233
                    ->isRequired()
234
                ->end()
235
                ->booleanNode('default')
236
                    ->info('Set the name of the default queue')
237
                    ->defaultValue(false)
238
                ->end()
239
240
                // RabbitMQ configuration
241
                ->scalarNode('routing_key')
242
                    ->info($routingKeyInfo)
243
                    ->defaultValue('')
244
                ->end()
245
                ->booleanNode('recover')
246
                    ->info($recoverInfo)
247
                    ->defaultValue(false)
248
                ->end()
249
                ->scalarNode('dead_letter_exchange')
250
                    ->info($deadLetterExchangeInfo)
251
                    ->defaultValue(null)
252
                ->end()
253
254
                // Database configuration (Doctrine)
255
                ->arrayNode('types')
256
                    ->info($typesInfo)
257
                    ->defaultValue(array())
258
                    ->prototype('scalar')->end()
259
                ->end()
260
            ->end();
261
262
        return $node;
263
    }
264
}
265