Completed
Pull Request — master (#40)
by Eugene
04:34
created

Configuration::addSpamAlertsSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 9.7333
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Azine\MailgunWebhooksBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
/**
10
 * This is the class that validates and merges configuration from your app/config files.
11
 *
12
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
13
 */
14
class Configuration implements ConfigurationInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 3
    public function getConfigTreeBuilder()
20
    {
21 3
        $treeBuilder = new TreeBuilder(AzineMailgunWebhooksExtension::PREFIX);
22 3
        $rootNode = $this->getRootNode($treeBuilder, AzineMailgunWebhooksExtension::PREFIX);
23
24
        $rootNode
25 3
            ->children()
26 3
                ->scalarNode(AzineMailgunWebhooksExtension::API_KEY)->isRequired()->cannotBeEmpty()->info('Your api-key for mailgun => see https://mailgun.com/cp')->end()
27 3
                ->scalarNode(AzineMailgunWebhooksExtension::PUBLIC_API_KEY)->defaultValue('')->info('Your public-api-key for mailgun => see https://mailgun.com/cp')->end()
28 3
                ->scalarNode(AzineMailgunWebhooksExtension::EMAIL_DOMAIN)->defaultValue('')->info('Your email domain configured on Mailgun')->end()
29 3
            ->end();
30
31 3
        $this->addSpamAlertsSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
32 3
        $this->addBlacklistCheckSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
33
34 3
        return $treeBuilder;
35
    }
36
37
    /**
38
     * @param ArrayNodeDefinition $node
39
     */
40 3
    private function addSpamAlertsSection(ArrayNodeDefinition $node)
41
    {
42
        $node
43 3
            ->children()
44 3
                ->arrayNode(AzineMailgunWebhooksExtension::SPAM_ALERTS_PREFIX)
45 3
                    ->addDefaultsIfNotSet()
46 3
                    ->canBeUnset()
47 3
                    ->children()
48 3
                        ->booleanNode(AzineMailgunWebhooksExtension::SEND_ENABLED)->defaultFalse()->info('Whether to send email notifications after receiving spam complaints')->end()
49 3
                        ->scalarNode(AzineMailgunWebhooksExtension::SEND_INTERVAL)->defaultValue('60')->info('Interval in minutes between sending of email notifications after receiving spam complaints')->end()
50 3
                        ->scalarNode(AzineMailgunWebhooksExtension::TICKET_ID)->defaultValue('')->info('Mailgun helpdesk ticket ID to request new IP address in case of spam complains')->end()
51 3
                        ->scalarNode(AzineMailgunWebhooksExtension::TICKET_SUBJECT)->defaultValue('IP on spam-list, please fix.')->info('Mailgun HelpDesk ticket subject')->end()
52 3
                        ->scalarNode(AzineMailgunWebhooksExtension::TICKET_MESSAGE)->defaultValue('It looks like my ip is on a spam-list. Please, assign a clean IP to my domain.')->info('Mailgun HelpDesk ticket subject')->end()
53 3
                        ->scalarNode(AzineMailgunWebhooksExtension::ALERTS_RECIPIENT_EMAIL)->defaultValue('')->info('Admin E-Mail to send notification about spam complaints')->end()
54 3
        ->end();
55 3
    }
56
57
    /**
58
     * @param ArrayNodeDefinition $node
59
     */
60 3
    private function addBlacklistCheckSection(ArrayNodeDefinition $node)
61
    {
62
        $node
63 3
            ->children()
64 3
                ->arrayNode(AzineMailgunWebhooksExtension::HETRIXTOOLS_PREFIX)
65 3
                    ->addDefaultsIfNotSet()
66 3
                    ->canBeUnset()
67 3
                    ->children()
68 3
                        ->scalarNode(AzineMailgunWebhooksExtension::BLACKLIST_CHECK_API_KEY)->defaultValue('')->info('Your public-api-key for hetrixtools => see https://hetrixtools.com/')->end()
69 3
                        ->scalarNode(AzineMailgunWebhooksExtension::BLACKLIST_CHECK_IP_URL)->defaultValue('https://api.hetrixtools.com/v2/<API_TOKEN>/blacklist-check/ipv4/<IP_ADDRESS>/')->info('Url for checking if ip is in blacklist => see https://docs.hetrixtools.com/blacklist-check-api/')->end()
70 3
        ->end();
71 3
    }
72
73
    /**
74
     * @param TreeBuilder $treeBuilder
75
     * @param $name
76
     */
77
    private function getRootNode(TreeBuilder $treeBuilder, $name)
78
    {
79
        // BC layer for symfony/config 4.1 and older
80
        if (! \method_exists($treeBuilder, 'getRootNode')) {
81
            return $treeBuilder->root($name);
82
        }
83
84
        return $treeBuilder->getRootNode();
85
    }
86
}
87