Completed
Push — master ( 37ded2...9ce938 )
by Nikola
10:29 queued 04:41
created

Configuration::getFileRepositoryDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.4286
cc 1
eloc 13
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('run_open_code.exchange_rate.repository.file_repository')
42 4
                    ->info('Service ID which is in charge for rates persistence.')
43 4
                ->end()
44 4
                ->append($this->getRatesDefinition())
45 4
                ->append($this->getProcessorsDefinition())
46 4
                ->append($this->getFileRepositoryDefinition())
47 4
                ->append($this->getViewDefinition())
48 4
            ->end()
49 4
        ->end();
50
51 4
        return $treeBuilder;
52
    }
53
54
    /**
55
     * Build configuration tree for rates.
56
     *
57
     * @return ArrayNodeDefinition
58
     */
59 4
    protected function getRatesDefinition()
60
    {
61 4
        $node = new ArrayNodeDefinition('rates');
62
63
        $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
                        ->scalarNode('alias')->defaultValue(null)->end()
72 4
                        ->arrayNode('extra')->end()
73 4
                    ->end()
74 4
                ->end()
75 4
            ->end();
76
77 4
        return $node;
78
    }
79
80
    /**
81
     * Build configuration tree for processors.
82
     *
83
     * @return ArrayNodeDefinition
84
     */
85 4
    protected function getProcessorsDefinition()
86
    {
87 4
        $node = new ArrayNodeDefinition('processors');
88
89
        $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...
90 4
            ->info('List of processors which ought to be executed after fetch process.')
91 4
            ->useAttributeAsKey('name')
92 4
                ->prototype('scalar')->end()
93 4
            ->end();
94
95 4
        return $node;
96
    }
97
98
    /**
99
     * Build configuration tree for repository.
100
     *
101
     * @return ArrayNodeDefinition
102
     */
103 4
    protected function getFileRepositoryDefinition()
104
    {
105 4
        $node = new ArrayNodeDefinition('file_repository');
106
107
        $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...
108 4
            ->info('Configuration for file repository (if used).')
109 4
            ->addDefaultsIfNotSet()
110 4
            ->children()
111 4
                ->scalarNode('path')
112 4
                ->info('Absolute path to file where database file will be stored.')
113 4
                ->defaultValue('%kernel.root_dir%/db/exchange_rates.dat')
114 4
                ->end()
115 4
            ->end()
116 4
        ->end();
117
118 4
        return $node;
119
    }
120
121
    /**
122
     * Build configuration tree for view (controller).
123
     *
124
     * @return ArrayNodeDefinition
125
     */
126 4
    protected function getViewDefinition()
127
    {
128 4
        $node = new ArrayNodeDefinition('view');
129
130
        $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...
131 4
            ->info('Configuration of administration interface.')
132 4
            ->addDefaultsIfNotSet()
133 4
            ->children()
134 4
                ->scalarNode('base_template')
135 4
                    ->info('Base decorator template.')
136 4
                    ->defaultValue('@ExchangeRate/base.html.twig')
137 4
                ->end()
138 4
                ->scalarNode('list')
139 4
                    ->info('Template for list view.')
140 4
                    ->defaultValue('@ExchangeRate/list.html.twig')
141 4
                ->end()
142 4
                ->scalarNode('new')
143 4
                    ->info('Template for create new exchange rate view.')
144 4
                    ->defaultValue('@ExchangeRate/new.html.twig')
145 4
                ->end()
146 4
                ->scalarNode('edit')
147 4
                    ->info('Template for edit exchange rate view.')
148 4
                    ->defaultValue('@ExchangeRate/edit.html.twig')
149 4
                ->end()
150 4
                ->scalarNode('date_format')
151 4
                    ->info('Date format in list view.')
152 4
                    ->defaultValue('Y-m-d')
153 4
                ->end()
154 4
                ->scalarNode('time_format')
155 4
                    ->info('Date/time format in list view.')
156 4
                    ->defaultValue('H:i')
157 4
                ->end()
158 4
                ->booleanNode('secure')->defaultValue(true)->end()
159 4
            ->end()
160 4
        ->end();
161
162 4
        return $node;
163
    }
164
}
165