|
1
|
|
|
<?php |
|
2
|
|
|
namespace ParcelValue\ApiClient\Domain\Shipments; |
|
3
|
|
|
|
|
4
|
|
|
use WebServCo\Api\JsonApi\Document; |
|
5
|
|
|
use WebServCo\Framework\Cli\Ansi; |
|
6
|
|
|
use WebServCo\Framework\Cli\Sgr; |
|
7
|
|
|
use WebServCo\Framework\CliResponse; |
|
8
|
|
|
use WebServCo\Framework\Http; |
|
9
|
|
|
|
|
10
|
|
|
final class ShipmentsCommand extends \ParcelValue\ApiClient\AbstractController |
|
11
|
|
|
{ |
|
12
|
|
|
protected $logger; |
|
13
|
|
|
protected $curlBrowser; |
|
14
|
|
|
|
|
15
|
|
|
protected $httpResponse; |
|
16
|
|
|
protected $requestHeaders; |
|
17
|
|
|
protected $responseStatus; |
|
18
|
|
|
protected $responseContent; |
|
19
|
|
|
protected $responseHeaders; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct(); |
|
24
|
|
|
|
|
25
|
|
|
$this->repository = new ShipmentsRepository($this->outputLoader); |
|
26
|
|
|
|
|
27
|
|
|
$this->logger = new \WebServCo\Framework\FileLogger( |
|
28
|
|
|
__FUNCTION__, |
|
29
|
|
|
sprintf('%svar/log/', $this->data('path/project', '')), |
|
30
|
|
|
$this->request() |
|
31
|
|
|
); |
|
32
|
|
|
$this->curlBrowser = new \WebServCo\Framework\CurlBrowser($this->logger); |
|
33
|
|
|
if (\WebServCo\Framework\Environment::ENV_DEV == $this->config()->getEnv()) { |
|
34
|
|
|
$this->curlBrowser->setSkipSSlVerification(true); |
|
35
|
|
|
} |
|
36
|
|
|
$this->curlBrowser->setRequestHeader('Accept', Document::CONTENT_TYPE); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function create($clientId, $clientKey, $serverKey) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->outputCli('', true); |
|
42
|
|
|
$this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
|
43
|
|
|
|
|
44
|
|
|
$url = sprintf( |
|
45
|
|
|
'%s%s/shipments', |
|
46
|
|
|
$this->config()->get('app/api/url'), |
|
47
|
|
|
$this->config()->get('app/api/version') |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$jwt = \ParcelValue\Api\AuthenticationToken::generate($clientId, $clientKey, $serverKey); |
|
51
|
|
|
$this->curlBrowser->setRequestHeader('Authorization', sprintf('Bearer %s', $jwt)); |
|
52
|
|
|
$this->curlBrowser->setRequestHeader('Content-Type', Document::CONTENT_TYPE); |
|
53
|
|
|
|
|
54
|
|
|
$shipment = $this->repository->getTestShipment(); |
|
55
|
|
|
|
|
56
|
|
|
$document = new Document(); |
|
57
|
|
|
$document->setData($shipment); |
|
58
|
|
|
$postData = $document->toJson(); |
|
59
|
|
|
|
|
60
|
|
|
$this->outputCli('', true); |
|
61
|
|
|
$this->outputCli(sprintf('REQUEST: POST %s', $url), true); |
|
62
|
|
|
$this->httpResponse = $this->curlBrowser->post($url, $postData); |
|
63
|
|
|
$this->requestHeaders = $this->curlBrowser->getRequestHeaders(); |
|
64
|
|
|
foreach ($this->requestHeaders as $key => $value) { |
|
65
|
|
|
$this->outputCli(sprintf('%s: %s', Ansi::sgr($key, [Sgr::BOLD]), $value), true); |
|
66
|
|
|
} |
|
67
|
|
|
$this->outputCli('', true); |
|
68
|
|
|
$this->outputCli($postData, true); |
|
69
|
|
|
|
|
70
|
|
|
$this->responseStatus = $this->httpResponse->getStatus(); |
|
71
|
|
|
$this->responseHeaders = $this->httpResponse->getHeaders(); |
|
72
|
|
|
$this->responseContent = $this->httpResponse->getContent(); |
|
73
|
|
|
|
|
74
|
|
|
$this->outputCli('', true); |
|
75
|
|
|
$this->outputCli( |
|
76
|
|
|
sprintf( |
|
77
|
|
|
'RESPONSE: %s', |
|
78
|
|
|
Ansi::sgr( |
|
79
|
|
|
sprintf( |
|
80
|
|
|
'%s %s', |
|
81
|
|
|
$this->responseStatus, |
|
82
|
|
|
Http::$statusCodes[$this->responseStatus] ?: null |
|
83
|
|
|
), |
|
84
|
|
|
[400 > $this->responseStatus ? Sgr::GREEN : sgr::RED] |
|
85
|
|
|
) |
|
86
|
|
|
), |
|
87
|
|
|
true |
|
88
|
|
|
); |
|
89
|
|
|
foreach ($this->responseHeaders as $key => $value) { |
|
90
|
|
|
$this->outputCli(sprintf('%s: %s', Ansi::sgr($key, [Sgr::BOLD]), $value), true); |
|
91
|
|
|
} |
|
92
|
|
|
$this->outputCli('', true); |
|
93
|
|
|
$this->outputCli($this->responseContent, true); |
|
94
|
|
|
|
|
95
|
|
|
$this->outputCli('', true); |
|
96
|
|
|
$this->outputCli( |
|
97
|
|
|
sprintf('Processed result: %s', json_encode(json_decode($this->responseContent, true), JSON_PRETTY_PRINT)), |
|
98
|
|
|
true |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
return new CliResponse('', true); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|