|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Loevgaard\Consignor\ShipmentServer\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Assert\Assert; |
|
6
|
|
|
use Buzz\Client\Curl; |
|
7
|
|
|
use Loevgaard\Consignor\ShipmentServer\Request\RequestInterface; |
|
8
|
|
|
use Loevgaard\Consignor\ShipmentServer\Response\ResponseInterface; |
|
9
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
|
10
|
|
|
use Psr\Http\Client\ClientInterface; |
|
11
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
12
|
|
|
use Psr\Http\Message\RequestInterface as PsrRequestInterface; |
|
13
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
|
15
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
|
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 ClientInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $httpClient; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var RequestFactoryInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $requestFactory; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var StreamFactoryInterface|null |
|
65
|
|
|
*/ |
|
66
|
|
|
private $streamFactory; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* This is the last psr request |
|
70
|
|
|
* |
|
71
|
|
|
* @var PsrRequestInterface|null |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $request; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* This is the last PSR response |
|
77
|
|
|
* |
|
78
|
|
|
* @var PsrResponseInterface|null |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $response; |
|
81
|
|
|
|
|
82
|
|
|
public function __construct( |
|
83
|
|
|
string $actor, |
|
84
|
|
|
string $key, |
|
85
|
|
|
string $environment = 'production', |
|
86
|
|
|
ClientInterface $httpClient = null, |
|
87
|
|
|
ResponseFactoryInterface $responseFactory = null, |
|
88
|
|
|
RequestFactoryInterface $requestFactory = null, |
|
89
|
|
|
StreamFactoryInterface $streamFactory = null |
|
90
|
|
|
) { |
|
91
|
|
|
$this->actor = $actor; |
|
92
|
|
|
$this->key = $key; |
|
93
|
|
|
|
|
94
|
|
|
$psr17Factory = new Psr17Factory(); |
|
95
|
|
|
|
|
96
|
|
|
if(null === $responseFactory) { |
|
97
|
|
|
$responseFactory = $psr17Factory; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if(null === $requestFactory) { |
|
101
|
|
|
$requestFactory = $psr17Factory; |
|
102
|
|
|
} |
|
103
|
|
|
$this->requestFactory = $requestFactory; |
|
104
|
|
|
|
|
105
|
|
|
if(null === $streamFactory) { |
|
106
|
|
|
$streamFactory = new $psr17Factory; |
|
107
|
|
|
} |
|
108
|
|
|
$this->streamFactory = $streamFactory; |
|
109
|
|
|
|
|
110
|
|
|
if(null === $httpClient) { |
|
111
|
|
|
$httpClient = new Curl($responseFactory); |
|
112
|
|
|
} |
|
113
|
|
|
$this->httpClient = $httpClient; |
|
114
|
|
|
|
|
115
|
|
|
Assert::that($environment)->choice([self::ENV_DEV, self::ENV_PRODUCTION]); |
|
116
|
|
|
$this->environment = $environment; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function doRequest(RequestInterface $request): ResponseInterface |
|
120
|
|
|
{ |
|
121
|
|
|
// resetting last request and response |
|
122
|
|
|
$this->request = null; |
|
123
|
|
|
$this->response = null; |
|
124
|
|
|
|
|
125
|
|
|
// deduce url |
|
126
|
|
|
$url = $this->environment === self::ENV_DEV ? $this->testServerUrl : $this->productionServerUrl; |
|
127
|
|
|
|
|
128
|
|
|
// convert body to post string and inject auth and command params |
|
129
|
|
|
$body = $request->getBody(); |
|
130
|
|
|
$body['actor'] = $this->actor; |
|
131
|
|
|
$body['key'] = $this->key; |
|
132
|
|
|
$body['command'] = $request->getCommand(); |
|
133
|
|
|
|
|
134
|
|
|
// create request |
|
135
|
|
|
$psrRequest = $this->requestFactory->createRequest('POST', $url); |
|
136
|
|
|
$psrRequest = $psrRequest |
|
137
|
|
|
->withHeader('Content-Type', 'application/x-www-form-urlencoded') |
|
138
|
|
|
->withHeader('Accept', 'application/json') |
|
139
|
|
|
->withBody($this->streamFactory->createStream(http_build_query($body))) |
|
140
|
|
|
; |
|
141
|
|
|
$this->request = $psrRequest; |
|
142
|
|
|
|
|
143
|
|
|
// send request |
|
144
|
|
|
$this->response = $this->httpClient->sendRequest($this->request); |
|
145
|
|
|
|
|
146
|
|
|
$responseClass = $request->getResponseClass(); |
|
147
|
|
|
|
|
148
|
|
|
return new $responseClass($this->response, $request); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getActor(): string |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->actor; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function getRequest(): ?PsrRequestInterface |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->request; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function getResponse(): ?PsrResponseInterface |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->response; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|