1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\NotificationService; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\CredentialsMapper; |
13
|
|
|
use Zbox\UnifiedPush\Exception\InvalidArgumentException; |
14
|
|
|
use Zbox\UnifiedPush\Exception\RuntimeException; |
15
|
|
|
use Zbox\UnifiedPush\Exception\DomainException; |
16
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\AuthToken; |
17
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\NullCredentials; |
18
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\SSLCertificate; |
19
|
|
|
|
20
|
|
|
class ServiceCredentialsFactory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var CredentialsMapper |
24
|
|
|
*/ |
25
|
|
|
protected $credentialsMapper; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $credentialsPath; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param CredentialsMapper $credentialsMapper |
39
|
|
|
*/ |
40
|
|
|
public function __construct(CredentialsMapper $credentialsMapper) |
41
|
|
|
{ |
42
|
|
|
$this->credentialsMapper = $credentialsMapper; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $credentialsPath |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function setCredentialsPath($credentialsPath) |
50
|
|
|
{ |
51
|
|
|
$this->credentialsPath = $credentialsPath; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $config |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function setConfig(array $config) |
60
|
|
|
{ |
61
|
|
|
$this->config = $config; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Load sender`s notification services credentials |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
* @throws DomainException |
70
|
|
|
*/ |
71
|
|
|
public function loadServiceCredentialsConfig() |
72
|
|
|
{ |
73
|
|
|
$configPath = $this->credentialsPath; |
74
|
|
|
|
75
|
|
|
if (!file_exists($configPath)) { |
76
|
|
|
throw new InvalidArgumentException( |
77
|
|
|
sprintf( |
78
|
|
|
"Credentials file '%s' doesn`t exists", |
79
|
|
|
$configPath |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$config = json_decode(file_get_contents($configPath), true); |
85
|
|
|
|
86
|
|
|
if (!is_array($config)) { |
87
|
|
|
throw new RuntimeException('Empty credentials config'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->config = $config; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the list of names of notification services |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function getInitializedServices() |
101
|
|
|
{ |
102
|
|
|
return array_keys($this->config); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns credentials for notification service |
107
|
|
|
* |
108
|
|
|
* @param string $serviceName |
109
|
|
|
* @throws DomainException |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getCredentialsByService($serviceName) |
113
|
|
|
{ |
114
|
|
|
if (empty($this->config)) { |
115
|
|
|
$this->loadServiceCredentialsConfig(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!in_array($serviceName, $this->getInitializedServices())) { |
119
|
|
|
throw new DomainException( |
120
|
|
|
sprintf("Credentials for service '%s' was not initialized", $serviceName) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return |
125
|
|
|
$this |
126
|
|
|
->credentialsMapper |
127
|
|
|
->mapCredentials( |
128
|
|
|
$this->getCredentialsDTOByServiceName($serviceName), |
|
|
|
|
129
|
|
|
$this->config[$serviceName] |
130
|
|
|
) |
131
|
|
|
; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $serviceName |
136
|
|
|
* @return \Zbox\UnifiedPush\Utils\ClientCredentials\CredentialsInterface |
137
|
|
|
*/ |
138
|
|
|
private function getCredentialsDTOByServiceName($serviceName) |
139
|
|
|
{ |
140
|
|
|
$credentialsType = NotificationServices::getCredentialsTypeByService($serviceName); |
141
|
|
|
|
142
|
|
|
switch ($credentialsType) { |
143
|
|
|
case NotificationServices::CREDENTIALS_CERTIFICATE: |
144
|
|
|
return new SSLCertificate(); |
145
|
|
|
break; |
|
|
|
|
146
|
|
|
|
147
|
|
|
case NotificationServices::CREDENTIALS_AUTH_TOKEN: |
148
|
|
|
return new AuthToken(); |
149
|
|
|
break; |
|
|
|
|
150
|
|
|
|
151
|
|
|
case NotificationServices::CREDENTIALS_NULL: |
152
|
|
|
return new NullCredentials(); |
153
|
|
|
break; |
|
|
|
|
154
|
|
|
|
155
|
|
|
default: |
156
|
|
|
throw new DomainException(sprintf("Unsupported credentials type '%s'", $credentialsType)); |
157
|
|
|
break; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.