Completed
Push — master ( b615a7...036546 )
by Nikola
05:36 queued 20s
created

Configuration::getSourcesDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 8
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) 2016 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 Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
13
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
14
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
16
/**
17
 * Class Configuration
18
 *
19
 * Configuration tree.
20
 *
21
 * @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection
22
 */
23
class Configuration implements ConfigurationInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 4
    public function getConfigTreeBuilder()
29
    {
30 4
        $treeBuilder = new TreeBuilder();
31
32 4
        $rootNode = $treeBuilder->root('run_open_code_exchange_rate');
33
34
        $rootNode
35 4
            ->children()
36 4
                ->scalarNode('base_currency')
37 4
                    ->isRequired()
38 4
                    ->info('Set base currency in which you are doing your business activities.')
39 4
                ->end()
40 4
                ->scalarNode('repository')
41 4
                    ->defaultValue('file')
42 4
                    ->info('Service ID which is in charge for rates persistence.')
43 4
                ->end()
44 4
                ->append($this->getRatesDefinition())
45 4
                ->append($this->getFileRepositoryDefinition())
46 4
                ->append($this->getSourcesDefinition())
47 4
                ->append($this->getViewDefinition())
48 4
                ->append($this->getNotificationDefinition())
49 4
            ->end()
50
        ->end();
51 4
52
        return $treeBuilder;
53
    }
54
55
    /**
56
     * Build configuration tree for rates.
57
     *
58
     * @return ArrayNodeDefinition
59 4
     */
60
    protected function getRatesDefinition()
61 4
    {
62
        $node = new ArrayNodeDefinition('rates');
63 4
        $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...
64 4
            ->info('Configuration of each individual rate with which you intend to work with.')
65 4
            ->requiresAtLeastOneElement()
66 4
                ->prototype('array')
67 4
                    ->children()
68 4
                        ->scalarNode('currency_code')->isRequired()->end()
69 4
                        ->scalarNode('rate_type')->isRequired()->end()
70 4
                        ->scalarNode('source')->isRequired()->end()
71 4
                        ->arrayNode('extra')->end()
72 4
                    ->end()
73 4
                ->end()
74
            ->end();
75 4
76
        return $node;
77
    }
78
79
    /**
80
     * Build configuration tree for repository.
81
     *
82
     * @return ArrayNodeDefinition
83 4
     */
84
    protected function getFileRepositoryDefinition()
85 4
    {
86
        $node = new ArrayNodeDefinition('file_repository');
87
88 4
        $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 addDefaultsIfNotSet() 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...
89 4
            ->info('Configuration for file repository (if used).')
90 4
            ->addDefaultsIfNotSet()
91 4
            ->children()
92 4
                ->scalarNode('path')
93 4
                ->info('Absolute path to file where database file will be stored.')
94 4
                ->defaultValue('%kernel.root_dir%/db/exchange_rates.dat')
95 4
                ->end()
96 4
            ->end()
97
        ->end();
98 4
99
        return $node;
100
    }
101
102
    /**
103
     * Build configuration tree for simple sources.
104
     *
105
     * @return ArrayNodeDefinition
106 4
     */
107
    protected function getSourcesDefinition()
108 4
    {
109
        $node = new ArrayNodeDefinition('sources');
110
111 4
        $node
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...
112 4
            ->info('Add sources to sources registry without registering them into service container.')
113 4
            ->useAttributeAsKey('name')
114 4
            ->prototype('scalar')->end()
115
        ->end();
116 4
117
        return $node;
118
    }
119
120
    /**
121
     * Build configuration tree for view (controller).
122
     *
123
     * @return ArrayNodeDefinition
124 4
     */
125
    protected function getViewDefinition()
126 4
    {
127
        $node = new ArrayNodeDefinition('view');
128
129 4
        $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 addDefaultsIfNotSet() 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...
130 4
            ->info('Configuration of administration interface.')
131 4
            ->addDefaultsIfNotSet()
132 4
            ->children()
133 4
                ->scalarNode('base_template')
134 4
                    ->info('Base decorator template.')
135 4
                    ->defaultValue('@ExchangeRate/admin/base.html.twig')
136 4
                ->end()
137 4
                ->scalarNode('list')
138 4
                    ->info('Template for list view.')
139 4
                    ->defaultValue('@ExchangeRate/admin/list.html.twig')
140 4
                ->end()
141 4
                ->scalarNode('new')
142 4
                    ->info('Template for create new exchange rate view.')
143 4
                    ->defaultValue('@ExchangeRate/admin/new.html.twig')
144 4
                ->end()
145 4
                ->scalarNode('edit')
146 4
                    ->info('Template for edit exchange rate view.')
147 4
                    ->defaultValue('@ExchangeRate/admin/edit.html.twig')
148 4
                ->end()
149 4
                ->scalarNode('date_format')
150 4
                    ->info('Date format in list view.')
151 4
                    ->defaultValue('Y-m-d')
152 4
                ->end()
153 4
                ->scalarNode('time_format')
154 4
                    ->info('Date/time format in list view.')
155 4
                    ->defaultValue('H:i')
156 4
                ->end()
157 4
                ->booleanNode('secure')->defaultValue(true)->end()
158 4
            ->end()
159
        ->end();
160 4
161
        return $node;
162
    }
163
164
    /**
165
     * Build configuration tree for e-mail notifications.
166
     *
167
     * @return ArrayNodeDefinition
168
     */
169
    public function getNotificationDefinition()
170
    {
171
        $node = new ArrayNodeDefinition('notifications');
172
173
        $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...
174
            ->info('Notification settings.')
175
            ->children()
176
                ->arrayNode('fetch')
177
                    ->children()
178
                        ->booleanNode('enabled')
179
                            ->info('Send e-mail report about fetch result and fetched rates.')
180
                            ->defaultFalse()
181
                        ->end()
182
                        ->arrayNode('to')
183
                            ->info('Recipients e-mail addresses.')
184
                        ->end()
185
                        ->arrayNode('to')
186
                            ->info('Blank carbon copy recipients e-mail addresses.')
187
                        ->end()
188
                        ->arrayNode('templates')
189
                            ->children()
190
                                ->scalarNode('success')
191
                                    ->isRequired()
192
                                    ->defaultValue('@ExchangeRate/mail/success.html.twig')
193
                                ->end()
194
                                ->scalarNode('error')
195
                                    ->isRequired()
196
                                    ->defaultValue('@ExchangeRate/mail/error.html.twig')
197
                                ->end()
198
                            ->end()
199
                        ->end()
200
                    ->end()
201
                ->end()
202
            ->end()
203
        ->end();
204
205
        return $node;
206
    }
207
}
208