1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
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 <contact@terryl.in>
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
use function is_array;
26

27
/*
28
 * Messenger Trait is loaded in Firewall instance only.
29
 */
30
trait MessengerTrait
31
{
32
    /**
33
     * Get options from the configuration file.
34
     * This method is same as `$this->getConfig()` but returning value from array directly.
35
     *
36
     * @param string $option  The option of the section in the the configuration.
37
     * @param string $section The section in the configuration.
38
     *
39
     * @return mixed
40
     */
41
    abstract protected function getOption(string $option, string $section = '');
42

43
    /**
44
     * Set the messenger modules.
45
     *
46
     * @return void
47
     */
48 3
    protected function setupMessengers(): void
49
    {
50
        $messengerList = [
51 3
            'telegram',
52
            'line_notify',
53
            'sendgrid',
54
            'native_php_mail',
55
            'smtp',
56
            'mailgun',
57
            'rocket_chat',
58
            'slack',
59
            'slack_webhook',
60
        ];
61

62 3
        foreach ($messengerList as $messenger) {
63 3
            $setting = $this->getOption($messenger, 'messengers');
64

65 3
            if (is_array($setting)) {
66

67
                // Initialize messenger instances from the factory/
68 3
                if (MessengerFactory::check($setting)) {
69
    
70 3
                    $this->kernel->setMessenger(
71 3
                        MessengerFactory::getInstance(
72
                            // The ID of the messenger module in the configuration.
73 3
                            $messenger, 
74
                            // The settings of the messenger module in the configuration.
75 2
                            $setting    
76
                        )
77
                    );
78
                }
79
            }
80

81 3
            unset($setting);
82
        }
83
    }
84

85
    /**
86
     * Set messenger events.
87
     *
88
     * @return void
89
     */
90 3
    protected function setupMessageEvents(): void
91
    {
92 3
        $setting = $this->getOption('failed_attempts_in_a_row', 'events');
93

94 3
        $notifyDataCircle = $setting['data_circle']['messenger'] ?: false;
95 3
        $notifySystemFirewall = $setting['system_firewall']['messenger'] ?: false;
96

97 3
        $this->kernel->setProperty(
98 3
            'deny_attempt_notify',
99
            [
100 3
                'data_circle'     => $notifyDataCircle,
101 3
                'system_firewall' => $notifySystemFirewall,
102
            ]
103
        );
104
    }
105
}

Read our documentation on viewing source code .

Loading