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