Passed
Push — 2.x ( fa7f46...ac8b38 )
by Terry
02:13
created

MessengerTrait::setupMessengers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 34
rs 9.6333
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
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Firewall\Messenger;
24
25
/*
26
 * Messenger Trait is loaded in Firewall instance only.
27
 */
28
trait MessengerTrait
29
{
30
    /**
31
     * Get options from the configuration file.
32
     * This method is same as `$this->getConfig()` but returning value from array directly.
33
     *
34
     * @param string $option  The option of the section in the the configuration.
35
     * @param string $section The section in the configuration.
36
     *
37
     * @return mixed
38
     */
39
    abstract protected function getOption(string $option, string $section = '');
40
41
    /**
42
     * Set the messenger modules.
43
     *
44
     * @return void
45
     */
46
    protected function setupMessengers(): void
47
    {
48
        $messengerList = [
49
            'telegram',
50
            'line_notify',
51
            'sendgrid',
52
            'native_php_mail',
53
            'smtp',
54
            'mailgun',
55
            'rocket_chat',
56
            'slack',
57
            'slack_webhook',
58
        ];
59
60
        foreach ($messengerList as $messenger) {
61
            $setting = $this->getOption($messenger, 'messengers');
62
63
            if (is_array($setting)) {
64
65
                // Initialize messenger instances from the factory/
66
                if (MessengerFactory::check($setting)) {
67
    
68
                    $this->kernel->setMessenger(
69
                        MessengerFactory::getInstance(
70
                            // The ID of the messenger module in the configuration.
71
                            $messenger, 
72
                            // The settings of the messenger module in the configuration.
73
                            $setting    
74
                        )
75
                    );
76
                }
77
            }
78
79
            unset($setting);
80
        }
81
    }
82
83
    /**
84
     * Set messenger events.
85
     *
86
     * @return void
87
     */
88
    protected function setupMessageEvents(): void
89
    {
90
        $setting = $this->getOption('failed_attempts_in_a_row', 'events');
91
92
        $notifyDataCircle = $setting['data_circle']['messenger'] ?: false;
93
        $notifySystemFirewall = $setting['system_firewall']['messenger'] ?: false;
94
95
        $this->kernel->setProperty(
96
            'deny_attempt_notify',
97
            [
98
                'data_circle'     => $notifyDataCircle,
99
                'system_firewall' => $notifySystemFirewall,
100
            ]
101
        );
102
    }
103
}
104