1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the fnayou/instapush-php project. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017. Aymen FNAYOU <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fnayou\InstapushPHP\Http; |
12
|
|
|
|
13
|
|
|
use Http\Client\Common\Plugin; |
14
|
|
|
use Http\Client\Common\PluginClient; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Configure an HTTP client. |
18
|
|
|
*/ |
19
|
|
|
class HttpClientConfigurator extends AbstractHttpClientConfigurator implements HttpClientConfiguratorInterface |
20
|
|
|
{ |
21
|
|
|
const USER_AGENT = 'fnayou/instapush-php'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $endpoint = 'http://api.instapush.im/v1'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $apiUserToken; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $apiAppIdentifier; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $apiAppSecret; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return \Http\Client\HttpClient |
45
|
|
|
*/ |
46
|
|
|
public function createConfiguredClient() |
47
|
|
|
{ |
48
|
|
|
$plugins = $this->getPrependPlugins(); |
49
|
|
|
$plugins[] = new Plugin\AddHostPlugin($this->getUriFactory()->createUri($this->getEndpoint())); |
50
|
|
|
$plugins[] = new Plugin\HeaderDefaultsPlugin($this->getHeaders()); |
51
|
|
|
|
52
|
|
|
return new PluginClient($this->getHttpClient(), \array_merge($plugins, $this->getAppendPlugins())); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getEndpoint() |
59
|
|
|
{ |
60
|
|
|
return $this->endpoint; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $endpoint |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function setEndpoint(string $endpoint) |
69
|
|
|
{ |
70
|
|
|
$this->endpoint = $endpoint; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getApiUserToken() |
79
|
|
|
{ |
80
|
|
|
return $this->apiUserToken; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $apiUserToken |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function setApiUserToken(string $apiUserToken = null) |
89
|
|
|
{ |
90
|
|
|
$this->apiUserToken = $apiUserToken; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getApiAppIdentifier() |
99
|
|
|
{ |
100
|
|
|
return $this->apiAppIdentifier; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $apiAppIdentifier |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setApiAppIdentifier(string $apiAppIdentifier = null) |
109
|
|
|
{ |
110
|
|
|
$this->apiAppIdentifier = $apiAppIdentifier; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getApiAppSecret() |
119
|
|
|
{ |
120
|
|
|
return $this->apiAppSecret; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $apiAppSecret |
125
|
|
|
* |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function setApiAppSecret(string $apiAppSecret = null) |
129
|
|
|
{ |
130
|
|
|
$this->apiAppSecret = $apiAppSecret; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
private function getHeaders() |
139
|
|
|
{ |
140
|
|
|
$headers = [ |
141
|
|
|
'User-Agent' => static::USER_AGENT, |
142
|
|
|
]; |
143
|
|
|
|
144
|
|
|
if (null !== $this->getApiUserToken()) { |
145
|
|
|
$headers['X-INSTAPUSH-TOKEN'] = $this->getApiUserToken(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (null !== $this->getApiAppIdentifier()) { |
149
|
|
|
$headers['X-INSTAPUSH-APPID'] = $this->getApiAppIdentifier(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (null !== $this->getApiAppSecret()) { |
153
|
|
|
$headers['X-INSTAPUSH-APPSECRET'] = $this->getApiAppSecret(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $headers; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|