Test Failed
Push — master ( 0fcd0f...4e8aa2 )
by Pavel
04:49 queued 16s
created

Configuration::configureEntityProto()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 8.8571
cc 1
eloc 26
nc 1
nop 1
1
<?php
2
3
namespace ScayTrase\Api\Cruds\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
final class Configuration implements ConfigurationInterface
10
{
11
    /** {@inheritdoc} */
12
    public function getConfigTreeBuilder()
13
    {
14
        $builder = new TreeBuilder();
15
        $root    = $builder->root('cruds');
16
17
        $root
18
            ->children()
19
            ->scalarNode('prefix')
20
            ->defaultNull()
21
            ->info('Route prefix')
22
            ->example('/api');
23
24
        $entities = $root->children()->arrayNode('entities');
25
        $entities->useAttributeAsKey('name');
26
        /** @var ArrayNodeDefinition $entitiesProto */
27
        $entitiesProto = $entities->prototype('array');
28
29
        $this->configureEntityProto($entitiesProto);
30
31
        return $builder;
32
    }
33
34
    private function configureEntityProto(ArrayNodeDefinition $parent)
35
    {
36
        $parent
37
            ->children()
38
            ->scalarNode('class')
39
            ->isRequired()
40
            ->info('Doctrine class')
41
            ->example('MyBundle:MyEntity');
42
43
        $parent
44
            ->children()
45
            ->scalarNode('prefix')
46
            ->defaultNull()
47
            ->info('Route prefix')
48
            ->example('/my-entity');
49
50
        $actions = $parent
51
            ->children()
52
            ->arrayNode('actions');
53
54
        $actions
55
            ->info('Action configuration')
56
            ->example(
57
                [
58
                    'read'   => null,
59
                    'create' => ['enabled' => false],
60
                    'delete' => ['enabled' => true, 'path' => '/remove'],
61
                ]
62
            );
63
64
        $this->configureCreateAction($actions);
0 ignored issues
show
Unused Code introduced by
The call to the method ScayTrase\Api\Cruds\Depe...configureCreateAction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
65
        $this->configureReadAction($actions);
66
        $this->configureUpdateAction($actions);
0 ignored issues
show
Unused Code introduced by
The call to the method ScayTrase\Api\Cruds\Depe...configureUpdateAction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
67
        $this->configureDeleteAction($actions);
0 ignored issues
show
Unused Code introduced by
The call to the method ScayTrase\Api\Cruds\Depe...configureDeleteAction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
68
    }
69
70
    private function configureCreateAction(ArrayNodeDefinition $parent)
0 ignored issues
show
Unused Code introduced by
The parameter $parent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
    }
73
74
    private function configureReadAction(ArrayNodeDefinition $parent)
75
    {
76
        $read = $parent
77
            ->children()
78
            ->arrayNode('read');
79
80
        $find = $read
81
            ->children()
82
            ->arrayNode('find');
83
84
        $get = $read
85
            ->children()
86
            ->arrayNode('get');
87
88
        $this->configureActionNode($find, 'find');
89
90
        $criteria = $find->children()->variableNode('criteria');
91
        $criteria
92
            ->defaultValue([])
93
            ->beforeNormalization()
94
            ->ifString()
95
            ->then(function ($v) {
96
                return [$v];
97
            })
98
            ->ifNull()
99
            ->thenEmptyArray();
100
101
        $criteria->info('List of prioritized criteria modifiers');
102
        $criteria->example(
103
            [
104
                '@my.criteria.modifier',
105
            ]
106
        );
107
108
        $this->configureActionNode($get, 'get');
109
    }
110
111
    private function configureUpdateAction(ArrayNodeDefinition $parent)
0 ignored issues
show
Unused Code introduced by
The parameter $parent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
    }
114
115
    private function configureDeleteAction(ArrayNodeDefinition $parent)
0 ignored issues
show
Unused Code introduced by
The parameter $parent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117
    }
118
119
    private function configureActionNode(ArrayNodeDefinition $parent, $action)
120
    {
121
        $parent
122
            ->children()
123
            ->scalarNode('path')
124
            ->example('/' . $action)
125
            ->info('Action path (prefixed)')
126
            ->defaultNull();
127
    }
128
}
129