Completed
Push — master ( 90ccc1...22512f )
by Kamil
35:43
created

ThemeConfiguration::addOptionalAuthorsList()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 26
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 17
nc 1
nop 1
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\ThemeBundle\Loader;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
18
/**
19
 * @author Kamil Kokot <[email protected]>
20
 */
21
final class ThemeConfiguration implements ConfigurationInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getConfigTreeBuilder()
27
    {
28
        $treeBuilder = new TreeBuilder();
29
        $rootNodeDefinition = $treeBuilder->root('sylius_theme');
30
31
        $rootNodeDefinition->ignoreExtraKeys();
32
33
        $this->addRequiredNameField($rootNodeDefinition);
34
        $this->addOptionalTitleField($rootNodeDefinition);
35
        $this->addOptionalDescriptionField($rootNodeDefinition);
36
        $this->addOptionalPathField($rootNodeDefinition);
37
        $this->addOptionalParentsList($rootNodeDefinition);
38
        $this->addOptionalAuthorsList($rootNodeDefinition);
39
40
        return $treeBuilder;
41
    }
42
43
    /**
44
     * @param ArrayNodeDefinition $rootNodeDefinition
45
     */
46
    private function addRequiredNameField(ArrayNodeDefinition $rootNodeDefinition)
47
    {
48
        $rootNodeDefinition->children()->scalarNode('name')->isRequired()->cannotBeEmpty();
49
    }
50
51
    /**
52
     * @param ArrayNodeDefinition $rootNodeDefinition
53
     */
54
    private function addOptionalTitleField(ArrayNodeDefinition $rootNodeDefinition)
55
    {
56
        $rootNodeDefinition->children()->scalarNode('title')->cannotBeEmpty();
57
    }
58
59
    /**
60
     * @param ArrayNodeDefinition $rootNodeDefinition
61
     */
62
    private function addOptionalDescriptionField(ArrayNodeDefinition $rootNodeDefinition)
63
    {
64
        $rootNodeDefinition->children()->scalarNode('description')->cannotBeEmpty();
65
    }
66
67
    /**
68
     * @param ArrayNodeDefinition $rootNodeDefinition
69
     */
70
    private function addOptionalPathField(ArrayNodeDefinition $rootNodeDefinition)
71
    {
72
        $rootNodeDefinition->children()->scalarNode('path')->cannotBeEmpty();
73
    }
74
75
    /**
76
     * @param ArrayNodeDefinition $rootNodeDefinition
77
     */
78
    private function addOptionalParentsList(ArrayNodeDefinition $rootNodeDefinition)
79
    {
80
        $this->deleteChildIfEmpty($rootNodeDefinition, 'parents');
81
82
        $parentsNodeDefinition = $rootNodeDefinition->children()->arrayNode('parents');
83
        $parentsNodeDefinition
84
            ->requiresAtLeastOneElement()
85
            ->performNoDeepMerging()
86
                ->prototype('scalar')
87
                ->cannotBeEmpty()
88
        ;
89
    }
90
91
    /**
92
     * @param ArrayNodeDefinition $rootNodeDefinition
93
     */
94
    private function addOptionalAuthorsList(ArrayNodeDefinition $rootNodeDefinition)
95
    {
96
        $this->deleteChildIfEmpty($rootNodeDefinition, 'authors');
97
98
        $authorsNodeDefinition = $rootNodeDefinition->children()->arrayNode('authors');
99
        $authorsNodeDefinition
100
            ->requiresAtLeastOneElement()
101
            ->performNoDeepMerging()
102
        ;
103
104
        /** @var ArrayNodeDefinition $authorNodeDefinition */
105
        $authorNodeDefinition = $authorsNodeDefinition->prototype('array');
106
        $authorNodeDefinition
107
            ->validate()
108
                ->ifTrue(function ($author) {
109
                    return [] === $author;
110
                })
111
                ->thenInvalid('Author cannot be empty!')
112
        ;
113
114
        $authorNodeBuilder = $authorNodeDefinition->children();
115
        $authorNodeBuilder->scalarNode('name')->cannotBeEmpty();
116
        $authorNodeBuilder->scalarNode('email')->cannotBeEmpty();
117
        $authorNodeBuilder->scalarNode('homepage')->cannotBeEmpty();
118
        $authorNodeBuilder->scalarNode('role')->cannotBeEmpty();
119
    }
120
121
    /**
122
     * @param ArrayNodeDefinition $arrayNodeDefinition
123
     * @param $field
124
     */
125
    private function deleteChildIfEmpty(ArrayNodeDefinition $arrayNodeDefinition, $field)
126
    {
127
        $arrayNodeDefinition
128
            ->validate()
129
                ->ifTrue(function ($data) use ($field) {
130
                    return isset($data[$field]) && ([] === $data[$field] || '' === $data[$field]);
131
                })
132
                ->then(function ($data) use ($field) {
133
                    unset($data[$field]);
134
135
                    return $data;
136
                })
137
        ;
138
    }
139
}
140