|
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\Kernel; |
|
24
|
|
|
|
|
25
|
|
|
use Shieldon\Messenger\Messenger\MessengerInterface; |
|
26
|
|
|
use RuntimeException; |
|
27
|
|
|
|
|
28
|
|
|
/* |
|
29
|
|
|
* Messenger Trait is loaded in Kernel instance only. |
|
30
|
|
|
*/ |
|
31
|
|
|
trait MessengerTrait |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Public methods | Desctiotion |
|
35
|
|
|
* ----------------------|--------------------------------------------- |
|
36
|
|
|
* setMessenger | Set a messenger |
|
37
|
|
|
* ----------------------|--------------------------------------------- |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The ways Shieldon send a message to when someone has been blocked. |
|
42
|
|
|
* The collection of \Shieldon\Messenger\Messenger\MessengerInterface |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $messenger = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The message that will be sent to the third-party API. |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $msgBody = ''; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get a class name without namespace string. |
|
57
|
|
|
* |
|
58
|
|
|
* @param object $instance Class |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
abstract protected function getClassName($instance): string; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set a messenger |
|
66
|
|
|
* |
|
67
|
|
|
* @param MessengerInterface $instance The messenger instance. |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
71 |
|
public function setMessenger(MessengerInterface $instance): void |
|
72
|
|
|
{ |
|
73
|
71 |
|
$class = $this->getClassName($instance); |
|
74
|
71 |
|
$this->messenger[$class] = $instance; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set the message body. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $message The message text. |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
1 |
|
protected function setMessageBody(string $message = ''): void |
|
85
|
|
|
{ |
|
86
|
1 |
|
$this->msgBody = $message; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// @codeCoverageIgnoreStart |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Undocumented function |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function triggerMessengers(): void |
|
97
|
|
|
{ |
|
98
|
|
|
if (empty($this->msgBody)) { |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
try { |
|
103
|
|
|
foreach ($this->messenger as $messenger) { |
|
104
|
|
|
$messenger->setTimeout(2); |
|
105
|
|
|
$messenger->send($this->msgBody); |
|
106
|
|
|
} |
|
107
|
|
|
// phpcs:ignore |
|
108
|
|
|
} catch (RuntimeException $e) { |
|
109
|
|
|
// Do not throw error, becasue the third-party services might be unavailable. |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// @codeCoverageIgnoreEnd |
|
114
|
|
|
} |
|
115
|
|
|
|