Passed
Push — 2.x ( be385f...a16b00 )
by Terry
01:58
created

MessengerTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setMessengers() 0 34 4
A setMessageEvents() 0 10 3
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Firewall\Messenger;
14
15
/*
16
 * Messenger Trait is loaded in Firewall instance only.
17
 */
18
trait MessengerTrait
19
{
20
    /**
21
     * Fetch value from configuration.
22
     *
23
     * @param string $option
24
     * @param string $section
25
     *
26
     * @return mixed
27
     */
28
    abstract function getOption(string $option, string $section = '');
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
30
    /**
31
     * Set the messenger modules.
32
     *
33
     * @return void
34
     */
35
    protected function setMessengers(): void
36
    {
37
        $messengerList = [
38
            'telegram',
39
            'line_notify',
40
            'sendgrid',
41
            'native_php_mail',
42
            'smtp',
43
            'mailgun',
44
            'rocket_chat',
45
            'slack',
46
            'slack_webhook',
47
        ];
48
49
        foreach ($messengerList as $messenger) {
50
            $setting = $this->getOption($messenger, 'messengers');
51
52
            if (is_array($setting)) {
53
54
                // Initialize messenger instances from the factory/
55
                if (MessengerFactory::check($messenger, $setting)) {
56
    
57
                    $this->kernel->add(
58
                        MessengerFactory::getInstance(
59
                            // The ID of the messenger module in the configuration.
60
                            $messenger, 
61
                            // The settings of the messenger module in the configuration.
62
                            $setting    
63
                        )
64
                    );
65
                }
66
            }
67
68
            unset($setting);
69
        }
70
    }
71
72
    /**
73
     * Set messenger events.
74
     *
75
     * @return void
76
     */
77
    protected function setMessageEvents(): void
78
    {
79
        $setting = $this->getOption('failed_attempts_in_a_row', 'events');
80
81
        $notifyDataCircle = $setting['data_circle']['messenger'] ?: false;
82
        $notifySystemFirewall = $setting['system_firewall']['messenger'] ?: false;
83
84
        $this->kernel->setProperty('deny_attempt_notify', [
85
            'data_circle' => $notifyDataCircle,
86
            'system_firewall' => $notifySystemFirewall,
87
        ]);
88
    }
89
}
90