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\CredentialsInterface; |
13
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\CredentialsMapper; |
14
|
|
|
use Zbox\UnifiedPush\Exception\InvalidArgumentException; |
15
|
|
|
use Zbox\UnifiedPush\Exception\RuntimeException; |
16
|
|
|
use Zbox\UnifiedPush\Exception\DomainException; |
17
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\AuthToken; |
18
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\NullCredentials; |
19
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\SSLCertificate; |
20
|
|
|
|
21
|
|
|
class ServiceCredentialsFactory |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var CredentialsMapper |
25
|
|
|
*/ |
26
|
|
|
protected $credentialsMapper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CredentialsInterface[] |
30
|
|
|
*/ |
31
|
|
|
protected $serviceCredentials; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param CredentialsMapper $credentialsMapper |
35
|
|
|
*/ |
36
|
|
|
public function __construct(CredentialsMapper $credentialsMapper) |
37
|
|
|
{ |
38
|
|
|
$this->credentialsMapper = $credentialsMapper; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $serviceName |
43
|
|
|
* @param array $credentials |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function addCredentialsForService($serviceName, $credentials) |
47
|
|
|
{ |
48
|
|
|
$credentialsDTO = |
49
|
|
|
$this |
50
|
|
|
->credentialsMapper |
51
|
|
|
->mapCredentials( |
52
|
|
|
$this->getCredentialsDTOByServiceName($serviceName), |
53
|
|
|
$credentials |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->serviceCredentials[$serviceName] = $credentialsDTO; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Load sender`s notification services credentials |
63
|
|
|
* |
64
|
|
|
* @param string $filePath |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function loadCredentialsFromFile($filePath) |
68
|
|
|
{ |
69
|
|
|
if (!file_exists($filePath)) { |
70
|
|
|
throw new InvalidArgumentException( |
71
|
|
|
sprintf( |
72
|
|
|
"Credentials file '%s' doesn`t exists", |
73
|
|
|
$filePath |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$config = json_decode(file_get_contents($filePath), true); |
79
|
|
|
|
80
|
|
|
if (!is_array($config)) { |
81
|
|
|
throw new RuntimeException('Empty credentials config'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
foreach ($config as $serviceName => $credentials) { |
85
|
|
|
$this->addCredentialsForService($serviceName, $credentials); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the list of names of notification services |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getInitializedServices() |
97
|
|
|
{ |
98
|
|
|
return array_keys($this->serviceCredentials); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns credentials for notification service |
103
|
|
|
* |
104
|
|
|
* @param string $serviceName |
105
|
|
|
* @throws DomainException |
106
|
|
|
* @return CredentialsInterface |
107
|
|
|
*/ |
108
|
|
|
public function getCredentialsByService($serviceName) |
109
|
|
|
{ |
110
|
|
|
if (!in_array($serviceName, $this->getInitializedServices())) { |
111
|
|
|
throw new DomainException( |
112
|
|
|
sprintf("Credentials for service '%s' was not initialized", $serviceName) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->serviceCredentials[$serviceName]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $serviceName |
121
|
|
|
* @return CredentialsInterface |
122
|
|
|
*/ |
123
|
|
|
private function getCredentialsDTOByServiceName($serviceName) |
124
|
|
|
{ |
125
|
|
|
$credentialsType = NotificationServices::getCredentialsTypeByService($serviceName); |
126
|
|
|
|
127
|
|
|
switch ($credentialsType) { |
128
|
|
|
case NotificationServices::CREDENTIALS_CERTIFICATE: |
129
|
|
|
return new SSLCertificate(); |
130
|
|
|
|
131
|
|
|
case NotificationServices::CREDENTIALS_AUTH_TOKEN: |
132
|
|
|
return new AuthToken(); |
133
|
|
|
|
134
|
|
|
case NotificationServices::CREDENTIALS_NULL: |
135
|
|
|
return new NullCredentials(); |
136
|
|
|
|
137
|
|
|
default: |
138
|
|
|
throw new DomainException(sprintf("Unsupported credentials type '%s'", $credentialsType)); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|