1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\MobilyWs; |
4
|
|
|
|
5
|
|
|
use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; |
6
|
|
|
|
7
|
|
|
class MobilyWsConfig |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array Supported authentication methods |
11
|
|
|
*/ |
12
|
|
|
protected $authenticationMethods = [ |
13
|
|
|
'apiKey', 'password', 'auto', |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string The authentication method |
18
|
|
|
*/ |
19
|
|
|
private $authMethod; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* MobilyWsConfig constructor. |
28
|
|
|
* |
29
|
|
|
* @param $config |
30
|
|
|
*/ |
31
|
19 |
|
public function __construct($config) |
32
|
|
|
{ |
33
|
19 |
|
$this->config = $config; |
34
|
19 |
|
$this->setAuthenticationMethod($config); |
35
|
17 |
|
$this->validateCredentials(); |
36
|
15 |
|
} |
37
|
|
|
|
38
|
9 |
|
public function getCredentials() |
39
|
|
|
{ |
40
|
9 |
|
switch ($this->authMethod) { |
41
|
9 |
|
case 'password': |
42
|
|
|
return [ |
43
|
2 |
|
'mobile' => $this->mobile, |
|
|
|
|
44
|
2 |
|
'password' => $this->password, |
|
|
|
|
45
|
2 |
|
]; |
46
|
7 |
|
case 'apiKey': |
47
|
|
|
return [ |
48
|
2 |
|
'apiKey' => $this->apiKey, |
|
|
|
|
49
|
2 |
|
]; |
50
|
5 |
|
case 'auto': |
51
|
5 |
|
return $this->getAutoCredentials(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function getAuthenticationMethod() |
56
|
|
|
{ |
57
|
1 |
|
return $this->authMethod; |
58
|
|
|
} |
59
|
|
|
|
60
|
11 |
|
public function __get($name) |
61
|
|
|
{ |
62
|
11 |
|
if ($name == 'request') { |
63
|
7 |
|
return $this->config['guzzle']['request']; |
64
|
|
|
} |
65
|
|
|
|
66
|
10 |
|
if (isset($this->config[$name])) { |
67
|
10 |
|
return $this->config[$name]; |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
|
71
|
19 |
|
protected function setAuthenticationMethod($config) |
72
|
|
|
{ |
73
|
19 |
|
if (isset($config['authentication'])) { |
74
|
18 |
|
if (in_array($config['authentication'], $this->authenticationMethods)) { |
75
|
17 |
|
return $this->authMethod = $config['authentication']; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
throw CouldNotSendNotification::withErrorMessage( |
79
|
1 |
|
sprintf('Method %s is not supported. Please choose from: (apiKey, password, auto)', |
80
|
1 |
|
$config['authentication'] |
81
|
1 |
|
) |
82
|
1 |
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
throw CouldNotSendNotification::withErrorMessage('Please set the authentication method in the mobilyws config file'); |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
|
protected function getAutoCredentials() |
89
|
|
|
{ |
90
|
5 |
|
if ($this->apiKey) { |
|
|
|
|
91
|
|
|
return [ |
92
|
4 |
|
'apiKey' => $this->apiKey, |
|
|
|
|
93
|
4 |
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return [ |
97
|
2 |
|
'mobile' => $this->mobile, |
|
|
|
|
98
|
2 |
|
'password' => $this->password, |
|
|
|
|
99
|
2 |
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
17 |
|
protected function validateCredentials() |
103
|
|
|
{ |
104
|
17 |
|
if (!isset($this->config['apiKey']) && !isset($this->config['mobile'], $this->config['password'])) { |
105
|
2 |
|
throw CouldNotSendNotification::withErrorMessage('No credentials were provided. Please set your (mobile/password) or apiKey in the config file'); |
106
|
|
|
} |
107
|
15 |
|
} |
108
|
|
|
} |
109
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.