|
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\DomainException; |
|
15
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\AuthToken; |
|
16
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\NullCredentials; |
|
17
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\SSLCertificate; |
|
18
|
|
|
|
|
19
|
|
|
class ServiceCredentialsFactory |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var CredentialsMapper |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $credentialsMapper; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var CredentialsInterface[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $serviceCredentials; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param CredentialsMapper $credentialsMapper |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(CredentialsMapper $credentialsMapper) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->credentialsMapper = $credentialsMapper; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $serviceName |
|
41
|
|
|
* @param array $credentials |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function addCredentialsForService($serviceName, $credentials) |
|
45
|
|
|
{ |
|
46
|
|
|
$credentialsDTO = |
|
47
|
|
|
$this |
|
48
|
|
|
->credentialsMapper |
|
49
|
|
|
->mapCredentials( |
|
50
|
|
|
$this->getCredentialsDTOByServiceName($serviceName), |
|
51
|
|
|
$credentials |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$this->serviceCredentials[$serviceName] = $credentialsDTO; |
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the list of names of notification services |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getInitializedServices() |
|
65
|
|
|
{ |
|
66
|
|
|
return array_keys($this->serviceCredentials); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns credentials for notification service |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $serviceName |
|
73
|
|
|
* @throws DomainException |
|
74
|
|
|
* @return CredentialsInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getCredentialsByService($serviceName) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!in_array($serviceName, $this->getInitializedServices())) { |
|
79
|
|
|
throw new DomainException( |
|
80
|
|
|
sprintf("Credentials for service '%s' was not initialized", $serviceName) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->serviceCredentials[$serviceName]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $serviceName |
|
89
|
|
|
* @return CredentialsInterface |
|
90
|
|
|
*/ |
|
91
|
|
|
private function getCredentialsDTOByServiceName($serviceName) |
|
92
|
|
|
{ |
|
93
|
|
|
$credentialsType = NotificationServices::getCredentialsTypeByService($serviceName); |
|
94
|
|
|
|
|
95
|
|
|
switch ($credentialsType) { |
|
96
|
|
|
case NotificationServices::CREDENTIALS_CERTIFICATE: |
|
97
|
|
|
return new SSLCertificate(); |
|
98
|
|
|
|
|
99
|
|
|
case NotificationServices::CREDENTIALS_AUTH_TOKEN: |
|
100
|
|
|
return new AuthToken(); |
|
101
|
|
|
|
|
102
|
|
|
case NotificationServices::CREDENTIALS_NULL: |
|
103
|
|
|
return new NullCredentials(); |
|
104
|
|
|
|
|
105
|
|
|
default: |
|
106
|
|
|
throw new DomainException(sprintf("Unsupported credentials type '%s'", $credentialsType)); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|