This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDispatcher.php
204 lines (165 loc) · 6.39 KB
/
Dispatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Artack\MxApi;
use Artack\MxApi\Hasher\HasherInterface;
use Artack\MxApi\Header\AcceptHeader;
use Artack\MxApi\Header\AcceptLanguageHeader;
use Artack\MxApi\Header\ContentTypeHeader;
use Artack\MxApi\Header\DateHeader;
use Artack\MxApi\Header\XAuthHeader;
use Artack\MxApi\Headers\HeadersInterface;
use Artack\MxApi\Normalizer\NormalizerInterface;
use Artack\MxApi\Randomizer\RandomizerInterface;
use Artack\MxApi\Request\Call;
use Artack\MxApi\Response\ApiResponse;
use Artack\MxApi\Response\ApiResponseInterface;
use Artack\MxApi\Response\NetworkResponse;
use Buzz\Client\ClientInterface;
use Buzz\Message\RequestInterface;
use DateTime;
use Exception;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Serializer;
/**
* @author Patrick Landolt <patrick.landolt@artack.ch>
*/
class Dispatcher
{
/**
* @var Configuration
*/
protected $configuration;
/**
* @var RandomizerInterface
*/
protected $randomizer;
/**
* @var NormalizerInterface
*/
protected $normalizer;
/**
* @var HasherInterface
*/
protected $hasher;
/**
* @var HeadersInterface
*/
protected $headers;
/**
* @var Call
*/
protected $call;
/**
* @var RequestInterface
*/
protected $request;
/**
* @var NetworkResponse
*/
protected $response;
/**
* @var ClientInterface
*/
protected $client;
/**
* @var ApiResponseInterface
*/
protected $apiResponse;
protected $serializer;
public function __construct(Configuration $configuration, RandomizerInterface $randomizer, NormalizerInterface $normalizer, HasherInterface $hasher, HeadersInterface $headers, RequestInterface $request, NetworkResponse $response, ClientInterface $client)
{
$this->configuration = $configuration;
$this->randomizer = $randomizer;
$this->normalizer = $normalizer;
$this->hasher = $hasher;
$this->headers = $headers;
$this->request = $request;
$this->response = $response;
$this->client = $client;
$encoders = array(new XmlEncoder(), new JsonEncoder());
$this->serializer = new Serializer(array(), $encoders);;
}
public function dispatch(Call $call)
{
$this->call = $call;
$this->prepare();
$this->call();
$this->parse();
return $this->apiResponse;
}
protected function prepare()
{
$this->call->setDate(new DateTime());
$this->call->setNonce($this->randomizer->getRandom(32));
if (!in_array($this->call->getMethod(), array('GET')) && count($this->call->getBody()))
{
$serializedBody = $this->serializer->encode($this->call->getBody(), $this->call->getFormat());
$this->call->setFormattedBody($serializedBody);
;
$this->headers->addHeader(new ContentTypeHeader($this->call->getPath(".", false), $this->configuration->getFormat(), $this->call->getVersion()));
}
$this->headers->addHeader(new DateHeader($this->call->getDate()));
$this->headers->addHeader(new AcceptHeader($this->call->getPath(".", false), $this->configuration->getFormat(), $this->call->getVersion()));
$this->headers->addHeader(new AcceptLanguageHeader($this->call->getLanguage()));
$serializedHashData = $this->normalizer->normalizeForHasher($this->call, $this->headers);
$hmac = $this->hasher->getHash($serializedHashData, $this->configuration->getApiSecret());
$this->headers->addHeader(new XAuthHeader($this->configuration->getCustomerKey(), $this->configuration->getApiKey(), $hmac, $this->call->getNonce()));
}
protected function call()
{
$this->request->setMethod($this->call->getMethod());
$this->request->setHost($this->call->getRequestPartBase());
$this->request->setResource($this->call->getRequestPartUri());
$this->request->setHeaders($this->headers->getHeaders());
if ($this->call->getFormattedBody())
{
$this->request->setContent($this->call->getFormattedBody());
}
$this->client->setIgnoreErrors(true);
$this->client->setVerifyPeer($this->configuration->getVerifyPeer());
$this->client->setMaxRedirects(0);
$this->client->setTimeout(60);
$this->client->send($this->request, $this->response);
}
protected function parse()
{
// var_dump($this->request);
// var_dump($this->response);
if ($this->response->isServerError())
{
throw new Exception(sprintf("API server error - statuscode [%s] with message [%s] / [%s]", $this->response->getStatusCode(), $this->response->getReasonPhrase(), $this->response->getContent()));
}
if ($this->response->isForbidden())
{
throw new Exception(sprintf("API call forbidden - statuscode [%s] with message [%s]", $this->response->getStatusCode(), $this->response->getReasonPhrase()));
}
$deSerializedBody = "";
if ($this->response->getContent()) {
$deSerializedBody = $this->serializer->decode($this->response->getContent(), $this->call->getFormat());
}
if(null === $deSerializedBody)
{
echo $this->response->getContent();
throw new \Exception("API response could not deserialize");
}
$this->apiResponse = new ApiResponse($this->response->getStatusCode(), $this->response->getReasonPhrase(), $this->response->getHeadersArray(), $deSerializedBody);
if(isset($deSerializedBody['status']) && $deSerializedBody['status']!= 'success')
{
throw new Exception(sprintf("API call error - statuscode [%s] with message [%s] / [%s]", $deSerializedBody['code'], $deSerializedBody['text'], $this->response->getContent()));
}
}
/**
* @param Configuration $configuration
*/
public function setConfiguration(Configuration $configuration)
{
$this->configuration = $configuration;
}
/**
* @return Configuration
*/
public function getConfiguration()
{
return $this->configuration;
}
}