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; |
12
|
|
|
|
13
|
|
|
use Http\Client\Common\Plugin; |
14
|
|
|
use Http\Client\Common\PluginClient; |
15
|
|
|
use Http\Client\HttpClient; |
16
|
|
|
use Http\Discovery\HttpClientDiscovery; |
17
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
18
|
|
|
use Http\Message\UriFactory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Configure an HTTP client. |
22
|
|
|
*/ |
23
|
|
|
class HttpClientConfigurator |
24
|
|
|
{ |
25
|
|
|
const USER_AGENT = 'fnayou/instapush-php'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $endpoint = 'https://api.instapush.im/v1'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $apiUserToken; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $apiAppIdentifier; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $apiAppSecret; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Http\Message\UriFactory |
49
|
|
|
*/ |
50
|
|
|
private $uriFactory; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Http\Client\HttpClient |
54
|
|
|
*/ |
55
|
|
|
private $httpClient; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private $prependPlugins = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
private $appendPlugins = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param \Http\Client\HttpClient $httpClient |
69
|
|
|
* @param \Http\Message\UriFactory $uriFactory |
70
|
|
|
*/ |
71
|
|
|
public function __construct(HttpClient $httpClient = null, UriFactory $uriFactory = null) |
72
|
|
|
{ |
73
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
74
|
|
|
$this->uriFactory = $uriFactory ?: UriFactoryDiscovery::find(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return \Http\Client\HttpClient |
79
|
|
|
*/ |
80
|
|
|
public function createConfiguredClient() |
81
|
|
|
{ |
82
|
|
|
$plugins = $this->prependPlugins; |
83
|
|
|
$plugins[] = new Plugin\AddHostPlugin($this->uriFactory->createUri($this->getEndpoint())); |
84
|
|
|
|
85
|
|
|
$plugins[] = new Plugin\HeaderDefaultsPlugin([ |
86
|
|
|
'User-Agent' => static::USER_AGENT, |
87
|
|
|
'X-INSTAPUSH-TOKEN' => $this->apiUserToken, |
88
|
|
|
'X-INSTAPUSH-APPID' => $this->apiAppIdentifier, |
89
|
|
|
'X-INSTAPUSH-APPSECRET' => $this->apiAppSecret, |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
return new PluginClient($this->httpClient, array_merge($plugins, $this->appendPlugins)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getEndpoint() |
99
|
|
|
{ |
100
|
|
|
return $this->endpoint; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $endpoint |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setEndpoint(string $endpoint) |
109
|
|
|
{ |
110
|
|
|
$this->endpoint = $endpoint; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $userToken |
117
|
|
|
* @param string $appIdentifier |
118
|
|
|
* @param string $appSecret |
119
|
|
|
* |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function setApiCredentials(string $userToken, string $appIdentifier, string $appSecret) |
123
|
|
|
{ |
124
|
|
|
$this->apiUserToken = $userToken; |
125
|
|
|
$this->apiAppIdentifier = $appIdentifier; |
126
|
|
|
$this->apiAppSecret = $appSecret; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param \Http\Client\Common\Plugin ...$plugin |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function appendPlugin(Plugin ...$plugin) |
137
|
|
|
{ |
138
|
|
|
foreach ($plugin as $p) { |
139
|
|
|
$this->appendPlugins[] = $p; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param \Http\Client\Common\Plugin ...$plugin |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function prependPlugin(Plugin ...$plugin) |
151
|
|
|
{ |
152
|
|
|
$plugin = \array_reverse($plugin); |
153
|
|
|
foreach ($plugin as $p) { |
154
|
|
|
\array_unshift($this->prependPlugins, $p); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|