Completed
Branch resource-configuration-builder (c854d7)
by Kamil
13:53
created

Configuration::addAttributeValueResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\AttributeBundle\DependencyInjection;
13
14
use Sylius\Bundle\AttributeBundle\Controller\AttributeController;
15
use Sylius\Bundle\AttributeBundle\Form\Type\AttributeTranslationType;
16
use Sylius\Bundle\AttributeBundle\Form\Type\AttributeType;
17
use Sylius\Bundle\AttributeBundle\Form\Type\AttributeValueType;
18
use Sylius\Bundle\ResourceBundle\DependencyInjection\Configuration\ResourceConfigurationGenerator;
19
use Sylius\Bundle\ResourceBundle\DependencyInjection\Configuration\ResourceConfigurationGeneratorInterface;
20
use Sylius\Bundle\ResourceBundle\DependencyInjection\Configuration\SyliusResource;
21
use Sylius\Bundle\ResourceBundle\DependencyInjection\Configuration\SyliusTranslationResource;
22
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceChoiceType;
23
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
24
use Sylius\Bundle\TranslationBundle\Doctrine\ORM\TranslatableResourceRepository;
25
use Sylius\Component\Attribute\Model\Attribute;
26
use Sylius\Component\Attribute\Model\AttributeInterface;
27
use Sylius\Component\Attribute\Model\AttributeTranslation;
28
use Sylius\Component\Attribute\Model\AttributeTranslationInterface;
29
use Sylius\Component\Translation\Factory\TranslatableFactory;
30
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
31
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
32
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
33
use Symfony\Component\Config\Definition\ConfigurationInterface;
34
35
/**
36
 * This class contains the configuration information for the bundle.
37
 *
38
 * This information is solely responsible for how the different configuration
39
 * sections are normalized, and merged.
40
 *
41
 * @author Paweł Jędrzejewski <[email protected]>
42
 */
43
class Configuration implements ConfigurationInterface
44
{
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getConfigTreeBuilder()
49
    {
50
        $treeBuilder = new TreeBuilder();
51
        $rootNode = $treeBuilder->root('sylius_attribute');
52
53
        $rootNode
54
            ->addDefaultsIfNotSet()
55
            ->children()
56
                ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
57
            ->end()
58
        ;
59
60
        $this->addResourcesSection($rootNode);
61
62
        return $treeBuilder;
63
    }
64
65
    /**
66
     * @param ArrayNodeDefinition $rootDefinition
67
     */
68
    private function addResourcesSection(ArrayNodeDefinition $rootDefinition)
69
    {
70
        $resourceConfigurationGenerator = new ResourceConfigurationGenerator();
71
72
        $resourcesDefinition = $resourceConfigurationGenerator->initResourcesConfiguration($rootDefinition);
73
        $resourcesDefinition->useAttributeAsKey('name');
74
75
        /** @var ArrayNodeDefinition $resourcesPrototypeDefinition */
76
        $resourcesPrototypeDefinition = $resourcesDefinition->prototype('array');
77
        $resourcesPrototypeDefinition->children()->scalarNode('subject')->isRequired();
78
79
        $this->addAttributeResource($resourceConfigurationGenerator, $resourcesPrototypeDefinition);
80
        $this->addAttributeValueResource($resourceConfigurationGenerator, $resourcesPrototypeDefinition);
81
    }
82
83
    /**
84
     * @param ResourceConfigurationGeneratorInterface $resourceConfigurationGenerator
85
     * @param ArrayNodeDefinition $resourcesDefinition
86
     */
87
    private function addAttributeResource(
88
        ResourceConfigurationGeneratorInterface $resourceConfigurationGenerator,
89
        ArrayNodeDefinition $resourcesDefinition
90
    ) {
91
        $attributeResource = new SyliusResource('attribute', Attribute::class, AttributeInterface::class);
92
        $attributeResource->useController(AttributeController::class);
93
        $attributeResource->useRepository(TranslatableResourceRepository::class);
94
        $attributeResource->useFactory(TranslatableFactory::class);
95
        $attributeResource->addForm('default', AttributeType::class, ['sylius']);
96
        $attributeResource->addForm('choice', ResourceChoiceType::class);
97
98
        $attributeTranslationResource = new SyliusTranslationResource(AttributeTranslation::class, AttributeTranslationInterface::class);
99
        $attributeTranslationResource->useDefaultController();
100
        $attributeTranslationResource->useDefaultRepository();
0 ignored issues
show
Unused Code introduced by
The call to the method Sylius\Bundle\ResourceBu...:useDefaultRepository() 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...
101
        $attributeTranslationResource->useDefaultFactory();
102
        $attributeTranslationResource->addForm('default', AttributeTranslationType::class, ['sylius']);
103
        $attributeTranslationResource->setTranslatableFields(['name']);
104
105
        $attributeResource->useTranslationResource($attributeTranslationResource);
106
107
        $resourceConfigurationGenerator->addSyliusResource($resourcesDefinition, $attributeResource);
108
    }
109
110
    /**
111
     * @param ResourceConfigurationGeneratorInterface $resourceConfigurationGenerator
112
     * @param ArrayNodeDefinition $resourcesDefinition
113
     */
114
    private function addAttributeValueResource(
115
        ResourceConfigurationGeneratorInterface $resourceConfigurationGenerator,
116
        ArrayNodeDefinition $resourcesDefinition
117
    ) {
118
        $attributeValueResource = new SyliusResource('attribute_value', null, null);
119
        $attributeValueResource->useDefaultController();
120
        $attributeValueResource->useDefaultRepository();
0 ignored issues
show
Unused Code introduced by
The call to the method Sylius\Bundle\ResourceBu...:useDefaultRepository() 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...
121
        $attributeValueResource->useDefaultFactory();
122
        $attributeValueResource->addForm('default', AttributeValueType::class, ['sylius']);
123
124
        $resourceConfigurationGenerator->addSyliusResource($resourcesDefinition, $attributeValueResource);
125
    }
126
}
127