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 = ''); |
|
|
|
|
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
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.