1
|
|
|
<?php namespace Ntholenaar\MultiSafepayClient; |
2
|
|
|
|
3
|
|
|
use Http\Client\Common\HttpMethodsClient; |
4
|
|
|
use Http\Client\Common\Plugin; |
5
|
|
|
use Http\Client\Common\PluginClient; |
6
|
|
|
use Http\Client\HttpClient; |
7
|
|
|
use Http\Discovery\HttpClientDiscovery; |
8
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
9
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
10
|
|
|
use Http\Message\MessageFactory; |
11
|
|
|
use Http\Message\UriFactory; |
12
|
|
|
use Ntholenaar\MultiSafepayClient\Http\Plugin\Authentication; |
13
|
|
|
use Ntholenaar\MultiSafepayClient\Http\Plugin\PrependPathPlugin; |
14
|
|
|
|
15
|
|
|
class Client implements ClientInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var HttpMethodsClient |
19
|
|
|
*/ |
20
|
|
|
private $methodsClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var MessageFactory |
24
|
|
|
*/ |
25
|
|
|
private $messageFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $environment = 'production'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var HttpClient |
34
|
|
|
*/ |
35
|
|
|
private $httpClient; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var UriFactory |
39
|
|
|
*/ |
40
|
|
|
private $uriFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $plugins = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param HttpClient|null $httpClient |
49
|
|
|
* @param MessageFactory|null $messageFactory |
50
|
|
|
* @param UriFactory|null $uriFactory |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
HttpClient $httpClient = null, |
54
|
|
|
MessageFactory $messageFactory = null, |
55
|
|
|
UriFactory $uriFactory = null |
56
|
|
|
) { |
57
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
58
|
|
|
|
59
|
|
|
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
60
|
|
|
|
61
|
|
|
$this->uriFactory = $uriFactory ?: UriFactoryDiscovery::find(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function setApiKey($apiKey) |
68
|
|
|
{ |
69
|
|
|
$this->addPlugin( |
70
|
|
|
new Plugin\AuthenticationPlugin( |
71
|
|
|
new Authentication($apiKey) |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the API endpoint. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function getApiEndpoint() |
84
|
|
|
{ |
85
|
|
|
if ($this->environment === 'test') { |
86
|
|
|
return 'https://testapi.multisafepay.com'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return 'https://api.multisafepay.com'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function environment($environment) |
96
|
|
|
{ |
97
|
|
|
if ($environment !== 'production' && $environment !== 'test') { |
98
|
|
|
throw new \InvalidArgumentException('Invalid environment specified.'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->environment = $environment; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function api($name) |
110
|
|
|
{ |
111
|
|
|
switch ($name) { |
112
|
|
|
case 'orders': |
113
|
|
|
return new Api\Orders($this); |
114
|
|
|
|
115
|
|
|
case 'gateways': |
116
|
|
|
return new Api\Gateways($this); |
117
|
|
|
|
118
|
|
|
case 'issuers': |
119
|
|
|
return new Api\Issuers($this); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
throw new \InvalidArgumentException('Invalid api specified.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function addPlugin(Plugin $plugin) |
129
|
|
|
{ |
130
|
|
|
$this->plugins[] = $plugin; |
131
|
|
|
|
132
|
|
|
$this->invalidHttpClient(); |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function getHttpClient() |
141
|
|
|
{ |
142
|
|
|
if ($this->methodsClient !== null) { |
143
|
|
|
return $this->methodsClient; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$plugins = array_merge( |
147
|
|
|
$this->plugins, |
148
|
|
|
array( |
149
|
|
|
new Plugin\ErrorPlugin(), |
150
|
|
|
new Plugin\AddHostPlugin($this->uriFactory->createUri($this->getApiEndpoint())), |
151
|
|
|
new PrependPathPlugin('/v1/json') |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$pluginClient = new PluginClient($this->httpClient, $plugins); |
156
|
|
|
|
157
|
|
|
$this->methodsClient = new HttpMethodsClient($pluginClient, $this->messageFactory); |
158
|
|
|
|
159
|
|
|
return $this->methodsClient; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Invalidate the http client. |
164
|
|
|
* |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
protected function invalidHttpClient() |
168
|
|
|
{ |
169
|
|
|
$this->methodsClient = null; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|