Completed
Push — master ( da405c...525739 )
by Emily
11s
created

Configuration::getConfigTreeBuilder()   B

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 75
    public function getConfigTreeBuilder()
21
    {
22 75
        $treeBuilder = new TreeBuilder();
23 75
        $rootNode = $treeBuilder->root('pixelart_swiftmailer_manipulator');
24
25
        $rootNode
26 75
            ->beforeNormalization()
27
                ->ifTrue(function ($v) {
28 75
                    return is_array($v)
29 75
                        && count($v) > 0
30 75
                        && !array_key_exists('mailers', $v)
31 75
                        && !array_key_exists('mailer', $v)
32 45
                    ;
33 75
                })
34 75
                ->then(function (array $v) {
35 30
                    $mailer = [];
36 30
                    foreach ($v as $key => $value) {
37 30
                        $mailer[$key] = $v[$key];
38 30
                        unset($v[$key]);
39 18
                    }
40
41 30
                    $v['mailers'] = ['default' => $mailer];
42
43 30
                    return $v;
44 75
                })
45 75
            ->end()
46 75
            ->children()
47 75
                ->append($this->getMailersNode())
48 75
            ->end()
49 75
            ->fixXmlConfig('mailer')
50
        ;
51
52 75
        return $treeBuilder;
53
    }
54
55
    /**
56
     * Return the mailers node.
57
     *
58
     * @return ArrayNodeDefinition
59
     */
60 75
    private function getMailersNode()
61
    {
62 75
        $treeBuilder = new TreeBuilder();
63 75
        $node = $treeBuilder->root('mailers');
64
65
        $node
66 75
            ->requiresAtLeastOneElement()
67 75
            ->useAttributeAsKey('name')
68 75
                ->prototype('array')
69 75
            ->children()
70 75
                ->scalarNode('prepend_subject')
71 75
                    ->info('String which is prepended onto the subject')
72 75
                ->end()
73 75
                ->scalarNode('prepend_body')
74 75
                    ->info('Path to template which is prepended onto the mail body')
75 75
                ->end()
76 75
                ->scalarNode('from_address')
77
                    ->info('The address message should be sent from')
78
                ->end()
79 75
80
            ->end()
81
        ;
82
83
        return $node;
84
    }
85
}
86