Completed
Push — azine-patch-1 ( 45cb64 )
by Dominik
21:23
created

Configuration::addBlacklistCheckSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.8666
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
                ->scalarNode(AzineMailgunWebhooksExtension::WEB_VIEW_ROUTE)->defaultValue('azine_admin_email_dashboard')->info('Route name to use to generate a WebView link in the Mailgun Event List.')->end()
30 3
                ->scalarNode(AzineMailgunWebhooksExtension::WEB_VIEW_TOKEN)->defaultValue(AzineMailgunWebhooksExtension::WEB_VIEW_TOKEN)->info('The name of the header for webview-token.')->end()
31 3
            ->end();
32
33 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...
34 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...
35
36 3
        return $treeBuilder;
37
    }
38
39
    /**
40
     * @param ArrayNodeDefinition $node
41
     */
42 3
    private function addSpamAlertsSection(ArrayNodeDefinition $node)
43
    {
44
        $node
45 3
            ->children()
46 3
                ->arrayNode(AzineMailgunWebhooksExtension::SPAM_ALERTS_PREFIX)
47 3
                    ->addDefaultsIfNotSet()
48 3
                    ->canBeUnset()
49 3
                    ->children()
50 3
                        ->booleanNode(AzineMailgunWebhooksExtension::SEND_ENABLED)->defaultFalse()->info('Whether to send email notifications after receiving spam complaints')->end()
51 3
                        ->scalarNode(AzineMailgunWebhooksExtension::SEND_INTERVAL)->defaultValue('60')->info('Interval in minutes between sending of email notifications after receiving spam complaints')->end()
52 3
                        ->scalarNode(AzineMailgunWebhooksExtension::TICKET_ID)->defaultValue('')->info('Mailgun helpdesk ticket ID to request new IP address in case of spam complains')->end()
53 3
                        ->scalarNode(AzineMailgunWebhooksExtension::TICKET_SUBJECT)->defaultValue('IP on spam-list, please fix.')->info('Mailgun HelpDesk ticket subject')->end()
54 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()
55 3
                        ->scalarNode(AzineMailgunWebhooksExtension::ALERTS_RECIPIENT_EMAIL)->defaultValue('')->info('Admin E-Mail to send notification about spam complaints')->end()
56 3
        ->end();
57 3
    }
58
59
    /**
60
     * @param ArrayNodeDefinition $node
61
     */
62 3
    private function addBlacklistCheckSection(ArrayNodeDefinition $node)
63
    {
64
        $node
65 3
            ->children()
66 3
                ->arrayNode(AzineMailgunWebhooksExtension::HETRIXTOOLS_PREFIX)
67 3
                    ->addDefaultsIfNotSet()
68 3
                    ->canBeUnset()
69 3
                    ->children()
70 3
                        ->scalarNode(AzineMailgunWebhooksExtension::BLACKLIST_CHECK_API_KEY)->defaultValue('')->info('Your public-api-key for hetrixtools => see https://hetrixtools.com/')->end()
71 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()
72 3
        ->end();
73 3
    }
74
75
    /**
76
     * @param TreeBuilder $treeBuilder
77
     * @param $name
78
     */
79 3
    private function getRootNode(TreeBuilder $treeBuilder, $name)
80
    {
81
        // BC layer for symfony/config 4.1 and older
82 3
        if (!\method_exists($treeBuilder, 'getRootNode')) {
83 3
            return $treeBuilder->root($name);
84
        }
85
86
        return $treeBuilder->getRootNode();
87
    }
88
}
89