Configuration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 25
cts 25
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 23
nc 1
nop 0
crap 5
1
<?php
2
3
/*
4
 * This file is part of the pixelart Swiftmailer manipulator bundle.
5
 *
6
 * (c) pixelart GmbH
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 Pixelart\Bundle\SwiftmailerManipulatorBundle\DependencyInjection;
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
class Configuration implements ConfigurationInterface
19
{
20 93
    public function getConfigTreeBuilder()
21
    {
22 93
        $treeBuilder = new TreeBuilder();
23 93
        $rootNode = $treeBuilder->root('pixelart_swiftmailer_manipulator');
24
25
        $rootNode
26 93
            ->beforeNormalization()
27
                ->ifTrue(function ($v) {
28 93
                    return is_array($v)
29 93
                        && count($v) > 0
30 93
                        && !array_key_exists('mailers', $v)
31 93
                        && !array_key_exists('mailer', $v)
32 60
                    ;
33 93
                })
34 93
                ->then(function (array $v) {
35 42
                    $mailer = [];
36 42
                    foreach ($v as $key => $value) {
37 42
                        $mailer[$key] = $v[$key];
38 42
                        unset($v[$key]);
39 27
                    }
40
41 42
                    $v['mailers'] = ['default' => $mailer];
42
43 42
                    return $v;
44 93
                })
45 93
            ->end()
46 93
            ->children()
47 93
                ->append($this->getMailersNode())
48 93
            ->end()
49 93
            ->fixXmlConfig('mailer')
50
        ;
51
52 93
        return $treeBuilder;
53
    }
54
55
    /**
56
     * Return the mailers node.
57
     *
58
     * @return ArrayNodeDefinition
59
     */
60 93
    private function getMailersNode()
61
    {
62 93
        $treeBuilder = new TreeBuilder();
63 93
        $node = $treeBuilder->root('mailers');
64
65
        $node
66 93
            ->requiresAtLeastOneElement()
67 93
            ->useAttributeAsKey('name')
68 93
                ->prototype('array')
69 93
            ->children()
70 93
                ->scalarNode('prepend_subject')
71 93
                    ->info('String which is prepended onto the subject')
72 93
                ->end()
73 93
                ->scalarNode('prepend_body')
74 93
                    ->info('Path to template which is prepended onto the mail body')
75 93
                ->end()
76 93
                ->scalarNode('from_address')
77 93
                    ->info('The address message should be sent from')
78 93
                ->end()
79
80 93
            ->end()
81
        ;
82
83 93
        return $node;
84
    }
85
}
86