Passed
Pull Request — master (#42)
by Martin
06:31 queued 04:13
created

Configuration::isBeforeSymfony4()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\SocialPostBundle\DependencyInjection;
6
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
/**
12
 * @since 1.0.0
13
 *
14
 * @license https://opensource.org/licenses/MIT
15
 *
16
 * @see https://github.com/martin-georgiev/social-post-bundle
17
 */
18
class Configuration implements ConfigurationInterface
19
{
20
    public function getConfigTreeBuilder(): TreeBuilder
21
    {
22
        if ($this->isBeforeSymfony4()) {
23
            $treeBuilder = new TreeBuilder();
0 ignored issues
show
Bug introduced by
The call to TreeBuilder::__construct() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
24
            $rootNode = $treeBuilder->root('social_post');
0 ignored issues
show
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
        } else {
26
            $treeBuilder = new TreeBuilder('social_post');
27
            $rootNode = $treeBuilder->getRootNode();
28
        }
29
30
        $rootNode
31
            ->children()
32
            ->arrayNode('publish_on')
33
            ->requiresAtLeastOneElement()
34
            ->prototype('enum')
35
            ->values(['facebook', 'linkedin', 'twitter'])
36
            ->end()
37
            ->end()
38
            ->end();
39
40
        $providers = $rootNode->children()->arrayNode('providers');
41
        $this->addFacebook($providers);
42
        $this->addLinkedIn($providers);
43
        $this->addTwitter($providers);
44
45
        return $treeBuilder;
46
    }
47
48
    private function isBeforeSymfony4(): bool
49
    {
50
        return !\method_exists(TreeBuilder::class, 'getRootNode');
51
    }
52
53
    private function addFacebook(ArrayNodeDefinition $node): void
54
    {
55
        $node
56
            ->children()
57
            ->arrayNode('facebook')
58
            ->children()
59
            ->scalarNode('app_id')
60
            ->isRequired()
61
            ->cannotBeEmpty()
62
            ->end()
63
            ->scalarNode('app_secret')
64
            ->isRequired()
65
            ->cannotBeEmpty()
66
            ->end()
67
            ->scalarNode('default_access_token')
68
            ->isRequired()
69
            ->cannotBeEmpty()
70
            ->end()
71
            ->scalarNode('page_id')
72
            ->isRequired()
73
            ->cannotBeEmpty()
74
            ->end()
75
            ->booleanNode('enable_beta_mode')
76
            ->defaultFalse()
77
            ->end()
78
            ->scalarNode('default_graph_version')
79
            ->defaultNull()
80
            ->end()
81
            ->enumNode('persistent_data_handler')
82
            ->values(['session', 'memory'])
83
            ->defaultValue('memory')
84
            ->end()
85
            ->enumNode('pseudo_random_string_generator')
86
            ->values(['mcrypt', 'openssl', 'urandom'])
87
            ->defaultValue('openssl')
88
            ->end()
89
            ->enumNode('http_client_handler')
90
            ->values(['curl', 'stream', 'guzzle'])
91
            ->defaultValue('curl')
92
            ->end()
93
            ->end()
94
            ->end();
95
    }
96
97
    private function addLinkedIn(ArrayNodeDefinition $node): void
98
    {
99
        $node
100
            ->children()
101
            ->arrayNode('linkedin')
102
            ->children()
103
            ->scalarNode('client_id')
104
            ->isRequired()
105
            ->cannotBeEmpty()
106
            ->end()
107
            ->scalarNode('client_secret')
108
            ->isRequired()
109
            ->cannotBeEmpty()
110
            ->end()
111
            ->scalarNode('access_token')
112
            ->isRequired()
113
            ->cannotBeEmpty()
114
            ->end()
115
            ->scalarNode('company_page_id')
116
            ->isRequired()
117
            ->cannotBeEmpty()
118
            ->end()
119
            ->end()
120
            ->end();
121
    }
122
123
    private function addTwitter(ArrayNodeDefinition $node): void
124
    {
125
        $node
126
            ->children()
127
            ->arrayNode('twitter')
128
            ->children()
129
            ->scalarNode('consumer_key')
130
            ->isRequired()
131
            ->cannotBeEmpty()
132
            ->end()
133
            ->scalarNode('consumer_secret')
134
            ->isRequired()
135
            ->cannotBeEmpty()
136
            ->end()
137
            ->scalarNode('access_token')
138
            ->isRequired()
139
            ->cannotBeEmpty()
140
            ->end()
141
            ->scalarNode('access_token_secret')
142
            ->isRequired()
143
            ->cannotBeEmpty()
144
            ->end()
145
            ->end()
146
            ->end();
147
    }
148
}
149