|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* A php library for using the Emarsys API. |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/quitoque/emarsys-php-client |
|
7
|
|
|
* @package emarsys-php-client |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
* @copyright Copyright (c) 2017 Quitoque <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Emarsys\HttpClient; |
|
13
|
|
|
|
|
14
|
|
|
use Emarsys\HttpClient\Message\Authentication\Wsse; |
|
15
|
|
|
use Http\Client\Common\Plugin; |
|
16
|
|
|
use Http\Client\Common\PluginClient; |
|
17
|
|
|
use Http\Client\HttpAsyncClient; |
|
18
|
|
|
use Http\Discovery\HttpAsyncClientDiscovery; |
|
19
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
20
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
|
21
|
|
|
use Http\Message\MessageFactory; |
|
22
|
|
|
use Http\Message\RequestFactory; |
|
23
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class Builder. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Claude Khedhiri <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Builder |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var HttpAsyncClient |
|
34
|
|
|
*/ |
|
35
|
|
|
private $httpClient; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $parameters; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var MessageFactory |
|
44
|
|
|
*/ |
|
45
|
|
|
private $requestFactory; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* A HTTP client with all our plugins. |
|
49
|
|
|
* |
|
50
|
|
|
* @var PluginClient |
|
51
|
|
|
*/ |
|
52
|
|
|
private $pluginClient; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* True if we should create a new Plugin client at next request. |
|
56
|
|
|
* |
|
57
|
|
|
* @var bool |
|
58
|
|
|
*/ |
|
59
|
|
|
private $httpClientModified = true; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var Plugin[] |
|
63
|
|
|
*/ |
|
64
|
|
|
private $plugins = array(); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array $parameters |
|
68
|
|
|
* @param HttpAsyncClient|null $httpAsyncClient |
|
69
|
|
|
* @param RequestFactory|null $requestFactory |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function __construct( |
|
72
|
|
|
array $parameters = array(), |
|
|
|
|
|
|
73
|
|
|
HttpAsyncClient $httpAsyncClient = null, |
|
74
|
|
|
RequestFactory $requestFactory = null |
|
75
|
|
|
) { |
|
76
|
4 |
|
$this->httpClient = $httpAsyncClient ?: HttpAsyncClientDiscovery::find(); |
|
77
|
4 |
|
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
|
|
|
|
|
|
78
|
4 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param HttpAsyncClient $httpAsyncClient |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setHttpClient(HttpAsyncClient $httpAsyncClient) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->httpClient = $httpAsyncClient; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param MessageFactory $requestFactory |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setRequestFactory($requestFactory) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->requestFactory = $requestFactory; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $parameters |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function setParameters(array $parameters) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$this->parameters = $parameters; |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function resolveParameters() |
|
105
|
|
|
{ |
|
106
|
|
|
$resolver = new OptionsResolver(); |
|
107
|
|
|
$resolver->setRequired('username'); |
|
108
|
|
|
$resolver->setRequired('secret'); |
|
109
|
|
|
|
|
110
|
|
|
$this->parameters = $resolver->resolve($this->parameters); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return PluginClient |
|
115
|
|
|
*/ |
|
116
|
3 |
|
public function getHttpClient() |
|
117
|
|
|
{ |
|
118
|
3 |
|
if ($this->httpClientModified) { |
|
119
|
3 |
|
$this->addPlugin(new Plugin\AuthenticationPlugin(new Wsse( |
|
120
|
3 |
|
$this->parameters['username'], |
|
121
|
3 |
|
$this->parameters['secret']) |
|
122
|
|
|
)); |
|
123
|
|
|
|
|
124
|
3 |
|
$this->addPlugin(new Plugin\BaseUriPlugin( |
|
125
|
3 |
|
UriFactoryDiscovery::find()->createUri('https://api.emarsys.net/api/v2')) |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
3 |
|
$this->addPlugin(new Plugin\HeaderDefaultsPlugin(array( |
|
129
|
3 |
|
'User-Agent' => 'emarsys-php-client (https://github.com/ck-developer/emarsys-php-client)', |
|
130
|
|
|
))); |
|
131
|
|
|
|
|
132
|
3 |
|
$this->httpClientModified = false; |
|
133
|
3 |
|
$this->pluginClient = new PluginClient($this->httpClient, $this->plugins); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
3 |
|
return $this->pluginClient; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return MessageFactory |
|
141
|
|
|
*/ |
|
142
|
2 |
|
public function getRequestFactory() |
|
143
|
|
|
{ |
|
144
|
2 |
|
return $this->requestFactory; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Add a new plugin to the end of the plugin chain. |
|
149
|
|
|
* |
|
150
|
|
|
* @param Plugin $plugin |
|
151
|
|
|
*/ |
|
152
|
3 |
|
public function addPlugin(Plugin $plugin) |
|
153
|
|
|
{ |
|
154
|
3 |
|
$this->plugins[get_class($plugin)] = $plugin; |
|
155
|
3 |
|
$this->httpClientModified = true; |
|
156
|
3 |
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Remove a plugin by its fully qualified class name. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $class |
|
162
|
|
|
*/ |
|
163
|
|
|
public function removePlugin($class) |
|
164
|
|
|
{ |
|
165
|
|
|
if (array_key_exists($class, $this->plugins)) { |
|
166
|
|
|
unset($this->plugins[$class]); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.