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
|
|
|
use Shieldon\Messenger\Messenger\MessengerInterface; |
16
|
|
|
|
17
|
|
|
/* |
18
|
|
|
* The factory creates messenger instances. |
19
|
|
|
*/ |
20
|
|
|
class MessengerFactory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Create a messenger instance. |
24
|
|
|
* |
25
|
|
|
* @param string $messenger The messenger's ID string. |
26
|
|
|
* @param array $setting The configuration of that messanger. |
27
|
|
|
* |
28
|
|
|
* @return MessengerInterface |
29
|
|
|
*/ |
30
|
|
|
public static function getInstance(string $messenger, array $setting): MessengerInterface |
31
|
|
|
{ |
32
|
|
|
$className = '\Shieldon\Firewall\Firewall\Messenger\Item' . self::getCamelCase($messenger); |
33
|
|
|
|
34
|
|
|
return $className::get($setting); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check whether a messenger is available or not. |
39
|
|
|
* |
40
|
|
|
* @param string $messenger The messenger's ID string. |
41
|
|
|
* @param array $setting The configuration of that messanger. |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public static function check(string $messenger, array $setting): bool |
46
|
|
|
{ |
47
|
|
|
// If the settings is not set correctly. |
48
|
|
|
if (empty($setting['enable']) || empty($setting['confirm_test'])) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// If the class doesn't exist. |
53
|
|
|
if (!file_exists(__DIR__ . '/' . self::getCamelCase($messenger) . '.php')) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Covert string with dashes into camel-case string. |
62
|
|
|
* |
63
|
|
|
* @param string $string A string with dashes. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public static function getCamelCase(string $string = '') |
68
|
|
|
{ |
69
|
|
|
$str = explode('-', $string); |
70
|
|
|
$str = implode('', array_map(function($word) { |
71
|
|
|
return ucwords($word); |
72
|
|
|
}, $str)); |
73
|
|
|
|
74
|
|
|
return $str; |
75
|
|
|
} |
76
|
|
|
} |