Completed
Push — master ( f17611...81469d )
by Nikola
04:23
created

getDoctrineDbalRepositoryDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This file is part of the Exchange Rate Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection;
11
12
use RunOpenCode\Bundle\ExchangeRate\Enum\Role;
13
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
14
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15
use Symfony\Component\Config\Definition\ConfigurationInterface;
16
17
/**
18
 * Class Configuration
19
 *
20
 * Configuration tree.
21
 *
22
 * @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection
23
 */
24
class Configuration implements ConfigurationInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 5
    public function getConfigTreeBuilder()
30
    {
31 5
        $treeBuilder = new TreeBuilder();
32
33 5
        $rootNode = $treeBuilder->root('run_open_code_exchange_rate');
34
35
        $rootNode
36 5
            ->children()
37 5
                ->scalarNode('base_currency')
38 5
                    ->isRequired()
39 5
                    ->info('Set base currency in which you are doing your business activities.') // TODO Validate!!!
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40 5
                ->end()
41 5
                ->scalarNode('repository')
42 5
                    ->defaultValue('file')
43 5
                    ->info('Service ID which is in charge for rates persistence.')
44 5
                ->end()
45 5
                ->append($this->getRatesDefinition())
46 5
                ->append($this->getFileRepositoryDefinition())
47 5
                ->append($this->getDoctrineDbalRepositoryDefinition())
48 5
                ->append($this->getSourcesDefinition())
49 5
                ->append($this->getAccessRolesDefinition())
50 5
                ->arrayNode('form_types')
51 5
                    ->addDefaultsIfNotSet()
52 5
                    ->children()
53 5
                        ->append($this->getSourceTypeDefinition())
54 5
                        ->append($this->getRateTypeTypeDefinition())
55 5
                        ->append($this->getCurrencyCodeTypeDefinition())
56 5
                        ->append($this->getRateTypeDefinition())
57 5
                    ->end()
58 5
                ->end()
59 5
            ->end()
60 5
        ->end();
61
62 5
        return $treeBuilder;
63
    }
64
65
    /**
66
     * Build configuration tree for rates.
67
     *
68
     * @return ArrayNodeDefinition
69
     */
70 5
    protected function getRatesDefinition()
71
    {
72 5
        $node = new ArrayNodeDefinition('rates');
73
        $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 children() 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...
74 5
            ->info('Configuration of each individual rate with which you intend to work with.')
75 5
            ->requiresAtLeastOneElement()
76 5
                ->prototype('array')
77 5
                    ->children()
78 5
                        ->scalarNode('currency_code')->isRequired()->end()
79 5
                        ->scalarNode('rate_type')->isRequired()->end()
80 5
                        ->scalarNode('source')->isRequired()->end()
81 5
                        ->arrayNode('extra')->end()
82 5
                    ->end()
83 5
                ->end()
84 5
            ->end();
85
86 5
        return $node;
87
    }
88
89
    /**
90
     * Build configuration tree for file repository.
91
     *
92
     * @return ArrayNodeDefinition
93
     */
94 5
    protected function getFileRepositoryDefinition()
95
    {
96 5
        $node = new ArrayNodeDefinition('file_repository');
97
98
        $node
99 5
            ->info('Configuration for file repository (if used).')
100 5
            ->addDefaultsIfNotSet()
101 5
            ->children()
102 5
                ->scalarNode('path')
103 5
                    ->info('Absolute path to file where database file will be stored.')
104 5
                    ->defaultValue('%kernel.root_dir%/db/exchange_rates.dat')
105 5
                ->end()
106 5
            ->end()
107 5
        ->end();
108
109 5
        return $node;
110
    }
111
112
    /**
113
     * Build configuration tree for Doctrine Dbal repository.
114
     *
115
     * @return ArrayNodeDefinition
116
     */
117 5
    protected function getDoctrineDbalRepositoryDefinition()
118
    {
119 5
        $node = new ArrayNodeDefinition('doctrine_dbal_repository');
120
121
        $node
122 5
            ->info('Configuration for Doctrine Dbla repository (if used).')
123 5
            ->addDefaultsIfNotSet()
124 5
            ->children()
125 5
                ->scalarNode('connection')
126 5
                    ->info('Which database connection to use.')
127 5
                    ->defaultValue('doctrine.dbal.default_connection')
128 5
                ->end()
129 5
                ->scalarNode('table_name')
130 5
                    ->info('Which table name to use for storing exchange rates.')
131 5
                    ->defaultValue('runopencode_exchange_rate')
132 5
                ->end()            
133 5
            ->end()
134 5
        ->end();
135
136 5
        return $node;
137
    }
138
139
    /**
140
     * Build configuration tree for simple sources.
141
     *
142
     * @return ArrayNodeDefinition
143
     */
144 5
    protected function getSourcesDefinition()
145
    {
146 5
        $node = new ArrayNodeDefinition('sources');
147
148
        $node
149 5
            ->info('Add sources to sources registry without registering them into service container.')
150 5
            ->useAttributeAsKey('name')
151 5
            ->prototype('scalar')->end()
152 5
        ->end();
153
154 5
        return $node;
155
    }
156
157
    /**
158
     * Build configuration tree for access roles.
159
     *
160
     * @return ArrayNodeDefinition
161
     */
162 5
    protected function getAccessRolesDefinition()
163
    {
164 5
        $node = new ArrayNodeDefinition('access_roles');
165
166
        $node
167 5
            ->info('Configuration of controller access roles.')
168 5
            ->addDefaultsIfNotSet()
169 5
            ->children()
170 5
                ->arrayNode('list')
171 5
                    ->defaultValue(array(Role::MANAGE_RATE, Role::VIEW_RATE))
172 5
                    ->prototype('scalar')->end()
173 5
                ->end()
174 5
                ->arrayNode('create')
175 5
                    ->defaultValue(array(Role::MANAGE_RATE, Role::VIEW_RATE))
176 5
                    ->prototype('scalar')->end()
177 5
                ->end()
178 5
                ->arrayNode('edit')
179 5
                    ->defaultValue(array(Role::MANAGE_RATE, Role::DELETE_RATE))
180 5
                    ->prototype('scalar')->end()
181 5
                ->end()
182 5
                ->arrayNode('delete')
183 5
                    ->defaultValue(array(Role::MANAGE_RATE, Role::DELETE_RATE))
184 5
                    ->prototype('scalar')->end()
185 5
                ->end()
186 5
            ->end()
187 5
        ->end();
188
189
190 5
        return $node;
191
    }
192
193
    /**
194
     * Build configuration tree for "RunOpenCode\Bundle\ExchangeRate\Form\Type\SourceType" default settings.
195
     *
196
     * @return ArrayNodeDefinition
197
     */
198 5 View Code Duplication
    protected function getSourceTypeDefinition()
0 ignored issues
show
Duplication introduced by
This method 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...
199
    {
200 5
        $node = new ArrayNodeDefinition('source_type');
201
202
        $node
203 5
            ->info('Modify default "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" settings.')
204 5
            ->addDefaultsIfNotSet()
205 5
            ->children()
206 5
                ->scalarNode('choice_translation_domain')->defaultValue('roc_exchange_rate')->end()
207 5
                ->arrayNode('preferred_choices')->end()
208 5
            ->end()
209 5
        ->end();
210
211 5
        return $node;
212
    }
213
214
    /**
215
     * Build configuration tree for "RunOpenCode\Bundle\ExchangeRate\Form\Type\RateTypeType" default settings.
216
     *
217
     * @return ArrayNodeDefinition
218
     */
219 5 View Code Duplication
    protected function getRateTypeTypeDefinition()
0 ignored issues
show
Duplication introduced by
This method 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...
220
    {
221 5
        $node = new ArrayNodeDefinition('rate_type_type');
222
223
        $node
224 5
            ->info('Modify default "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" settings.')
225 5
            ->addDefaultsIfNotSet()
226 5
            ->children()
227 5
                ->scalarNode('choice_translation_domain')->defaultValue('roc_exchange_rate')->end()
228 5
                ->arrayNode('preferred_choices')->end()
229 5
            ->end()
230 5
        ->end();
231
232 5
        return $node;
233
    }
234
235
    /**
236
     * Build configuration tree for "RunOpenCode\Bundle\ExchangeRate\Form\Type\CurrencyCodeType" default settings.
237
     *
238
     * @return ArrayNodeDefinition
239
     */
240 5 View Code Duplication
    protected function getCurrencyCodeTypeDefinition()
0 ignored issues
show
Duplication introduced by
This method 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...
241
    {
242 5
        $node = new ArrayNodeDefinition('currency_code_type');
243
244
        $node
245 5
            ->info('Modify default "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" settings.')
246 5
            ->addDefaultsIfNotSet()
247 5
            ->children()
248 5
                ->scalarNode('choice_translation_domain')->defaultValue('roc_exchange_rate')->end()
249 5
                ->arrayNode('preferred_choices')->end()
250 5
            ->end()
251 5
        ->end();
252
253 5
        return $node;
254
    }
255
256
    /**
257
     * Build configuration tree for "RunOpenCode\Bundle\ExchangeRate\Form\Type\RateType" default settings.
258
     *
259
     * @return ArrayNodeDefinition
260
     */
261 5
    protected function getRateTypeDefinition()
262
    {
263 5
        $node = new ArrayNodeDefinition('rate_type');
264
265
        $node
266 5
            ->info('Modify default "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" settings.')
267 5
            ->addDefaultsIfNotSet()
268 5
            ->children()
269 5
                ->scalarNode('choice_translation_domain')->defaultValue('roc_exchange_rate')->end()
270 5
                ->scalarNode('label_format')->defaultValue('{{currency-code}}, {{rate-type}} ({{source}})')->end()
271 5
                ->arrayNode('preferred_choices')->end()
272 5
            ->end()
273 5
        ->end();
274
275 5
        return $node;
276
    }
277
}
278