1
|
|
|
<?php |
2
|
|
|
namespace Loevgaard\Consignor\ShipmentServer\Client; |
3
|
|
|
|
4
|
|
|
use Assert\Assert; |
5
|
|
|
use Http\Client\Common\Plugin\ErrorPlugin; |
6
|
|
|
use Http\Client\Common\PluginClient; |
7
|
|
|
use Http\Client\HttpClient; |
8
|
|
|
use Http\Discovery\HttpClientDiscovery; |
9
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
10
|
|
|
use Http\Message\RequestFactory; |
11
|
|
|
use Loevgaard\Consignor\ShipmentServer\Request\RequestInterface; |
12
|
|
|
use Loevgaard\Consignor\ShipmentServer\Response\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\RequestInterface as PsrRequestInterface; |
14
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
15
|
|
|
use function GuzzleHttp\Psr7\build_query; |
16
|
|
|
|
17
|
|
|
class Client |
18
|
|
|
{ |
19
|
|
|
const ENV_DEV = 'dev'; |
20
|
|
|
const ENV_PRODUCTION = 'production'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This URL is used for testing purposes |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $testServerUrl = 'http://sstest.consignor.com/ship/ShipmentServerModule.dll'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* This URL is used for production requests |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $productionServerUrl = 'https://www.shipmentserver.com/ship/ShipmentServerModule.dll'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $actor; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $key; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Should be either 'dev' or 'production' |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $environment; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var HttpClient |
55
|
|
|
*/ |
56
|
|
|
protected $httpClient; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var RequestFactory |
60
|
|
|
*/ |
61
|
|
|
protected $requestFactory; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* This is the last request |
65
|
|
|
* |
66
|
|
|
* @var PsrRequestInterface|null |
67
|
|
|
*/ |
68
|
|
|
protected $request; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* This is the last PSR response (differs from the response returned form the doRequest method) |
72
|
|
|
* |
73
|
|
|
* @var PsrResponseInterface|null |
74
|
|
|
*/ |
75
|
|
|
protected $response; |
76
|
|
|
|
77
|
1 |
|
public function __construct(string $actor, string $key, array $plugins = [], HttpClient $httpClient = null, RequestFactory $requestFactory = null, string $environment = 'production') |
78
|
|
|
{ |
79
|
1 |
|
$this->actor = $actor; |
80
|
1 |
|
$this->key = $key; |
81
|
|
|
|
82
|
1 |
|
$plugins[] = new ErrorPlugin(); |
83
|
1 |
|
$this->httpClient = new PluginClient($httpClient ?: HttpClientDiscovery::find(), $plugins); |
84
|
1 |
|
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
85
|
|
|
|
86
|
1 |
|
Assert::that($environment)->choice([self::ENV_DEV, self::ENV_PRODUCTION]); |
87
|
1 |
|
$this->environment = $environment; |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param RequestInterface $request |
92
|
|
|
* @return ResponseInterface |
93
|
|
|
*/ |
94
|
1 |
|
public function doRequest(RequestInterface $request) : ResponseInterface |
95
|
|
|
{ |
96
|
|
|
// resetting last request and response |
97
|
1 |
|
$this->request = null; |
98
|
1 |
|
$this->response = null; |
99
|
|
|
|
100
|
|
|
// deduce url |
101
|
1 |
|
$url = $this->environment === self::ENV_DEV ? $this->testServerUrl : $this->productionServerUrl; |
102
|
|
|
|
103
|
|
|
// convert body to post string and inject auth and command params |
104
|
1 |
|
$body = $request->getBody(); |
105
|
1 |
|
$body['actor'] = $this->actor; |
106
|
1 |
|
$body['key'] = $this->key; |
107
|
1 |
|
$body['command'] = $request->getCommand(); |
108
|
1 |
|
$body = build_query($body); |
109
|
|
|
|
110
|
|
|
// set headers |
111
|
|
|
$headers = [ |
112
|
1 |
|
'Content-Type' => 'application/x-www-form-urlencoded', |
113
|
|
|
'Accept' => 'application/json' |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
// create request |
117
|
|
|
// @todo this is not necessarily the real request, since it can be manipulated by plugins, figure out a way to retrieve the correct one |
118
|
1 |
|
$this->request = $this->requestFactory->createRequest('POST', $url, $headers, $body); |
119
|
|
|
|
120
|
|
|
// send request |
121
|
1 |
|
$this->response = $this->httpClient->sendRequest($this->request); |
122
|
|
|
|
123
|
1 |
|
$responseClass = $request->getResponseClass(); |
124
|
|
|
|
125
|
1 |
|
return new $responseClass($this->response); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
1 |
|
public function getActor(): string |
132
|
|
|
{ |
133
|
1 |
|
return $this->actor; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return null|PsrRequestInterface |
138
|
|
|
*/ |
139
|
1 |
|
public function getRequest(): ?PsrRequestInterface |
140
|
|
|
{ |
141
|
1 |
|
return $this->request; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return null|PsrResponseInterface |
146
|
|
|
*/ |
147
|
1 |
|
public function getResponse(): ?PsrResponseInterface |
148
|
|
|
{ |
149
|
1 |
|
return $this->response; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|