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
|
|
|
|
17
|
|
|
class ServiceCredentialsFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $credentialsPath; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param CredentialsMapper $credentialsMapper |
31
|
|
|
*/ |
32
|
|
|
public function __construct(CredentialsMapper $credentialsMapper) |
33
|
|
|
{ |
34
|
|
|
$this->credentialsMapper = $credentialsMapper; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $credentialsPath |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function setCredentialsPath($credentialsPath) |
42
|
|
|
{ |
43
|
|
|
$this->credentialsPath = $credentialsPath; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Load sender`s notification services credentials |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
* @throws DomainException |
52
|
|
|
*/ |
53
|
|
|
public function loadServiceCredentialsConfig() |
54
|
|
|
{ |
55
|
|
|
$configPath = $this->credentialsPath; |
56
|
|
|
|
57
|
|
|
if (!file_exists($configPath)) { |
58
|
|
|
throw new InvalidArgumentException( |
59
|
|
|
sprintf( |
60
|
|
|
"Credentials file '%s' doesn`t exists", |
61
|
|
|
$configPath |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$config = json_decode(file_get_contents($configPath), true); |
67
|
|
|
|
68
|
|
|
if (empty($config)) { |
69
|
|
|
throw new RuntimeException('Empty credentials config'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->config = $config; |
|
|
|
|
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the list of names of notification services |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getInitializedServices() |
83
|
|
|
{ |
84
|
|
|
return array_keys($this->config); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns credentials for notification service |
89
|
|
|
* |
90
|
|
|
* @param string $serviceName |
91
|
|
|
* @throws DomainException |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getCredentialsByService($serviceName) |
95
|
|
|
{ |
96
|
|
|
if (empty($this->config)) { |
97
|
|
|
$this->config = $this->loadServiceCredentialsConfig(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!in_array($serviceName, $this->getInitializedServices())) { |
101
|
|
|
throw new DomainException( |
102
|
|
|
sprintf("Credentials for service '%s' was not initialized", $serviceName) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return |
107
|
|
|
$this |
108
|
|
|
->credentialsMapper |
109
|
|
|
->mapCredentials( |
110
|
|
|
NotificationServices::getCredentialsTypeByService($serviceName), |
111
|
|
|
$this->config[$serviceName] |
112
|
|
|
) |
113
|
|
|
; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: