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 $jwt; |
13
|
|
|
protected $headers; |
14
|
|
|
|
15
|
|
|
use \ParcelValue\ApiClient\Traits\ControllerApiTrait; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
parent::__construct(); |
20
|
|
|
|
21
|
|
|
$this->repository = new ShipmentsRepository($this->outputLoader); |
22
|
|
|
|
23
|
|
|
$this->validateApiConfig(); |
24
|
|
|
|
25
|
|
|
$this->jwt = \ParcelValue\Api\AuthenticationToken::generate( |
26
|
|
|
$this->clientId, |
27
|
|
|
$this->clientKey, |
28
|
|
|
$this->serverKey |
29
|
|
|
); |
30
|
|
|
$this->headers = ['Authorization' => sprintf('Bearer %s', $this->jwt)]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function create() |
34
|
|
|
{ |
35
|
|
|
$this->outputCli(Ansi::clear(), true); |
36
|
|
|
$this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
37
|
|
|
|
38
|
|
|
$this->initApiCall(); |
39
|
|
|
|
40
|
|
|
$url = sprintf('%s%s/shipments', $this->apiUrl, $this->apiVersion); |
41
|
|
|
|
42
|
|
|
$shipment = $this->repository->getShipment(); |
43
|
|
|
$document = new Document(); |
44
|
|
|
$document->setData($shipment); |
45
|
|
|
|
46
|
|
|
$this->headers['Content-Type'] = Document::CONTENT_TYPE; |
47
|
|
|
|
48
|
|
|
$this->handleApiCall($url, Http::METHOD_POST, $this->headers, $document->toJson()); |
49
|
|
|
|
50
|
|
|
return new CliResponse('', true); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function retrieve($shipmentId) |
54
|
|
|
{ |
55
|
|
|
$this->outputCli(Ansi::clear(), true); |
56
|
|
|
$this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
57
|
|
|
|
58
|
|
|
$this->initApiCall(); |
59
|
|
|
|
60
|
|
|
$url = sprintf('%s%s/shipments/%s', $this->apiUrl, $this->apiVersion, $shipmentId); |
61
|
|
|
|
62
|
|
|
$this->handleApiCall($url, Http::METHOD_GET, $this->headers); |
63
|
|
|
|
64
|
|
|
return new CliResponse('', true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function downloadDocuments($shipmentId) |
68
|
|
|
{ |
69
|
|
|
$this->outputCli(Ansi::clear(), true); |
70
|
|
|
$this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
71
|
|
|
|
72
|
|
|
$this->initApiCall(); |
73
|
|
|
|
74
|
|
|
$url = sprintf('%s%s/shipments/%s/documents', $this->apiUrl, $this->apiVersion, $shipmentId); |
75
|
|
|
|
76
|
|
|
$this->handleApiCall($url, Http::METHOD_GET, $this->headers); |
77
|
|
|
|
78
|
|
|
$data = json_decode($this->responseContent, true); |
79
|
|
|
if (isset($data['data']['attributes']['fileData']) && isset($data['data']['attributes']['fileName'])) { |
80
|
|
|
$filePath = sprintf( |
81
|
|
|
'%svar/tmp/%s', |
82
|
|
|
$this->config()->get('app/path/project'), |
83
|
|
|
$data['data']['attributes']['fileName'] |
84
|
|
|
); |
85
|
|
|
try { |
86
|
|
|
file_put_contents($filePath, base64_decode($data['data']['attributes']['fileData'])); |
87
|
|
|
$this->outputCli(Ansi::sgr(sprintf('Shipment documents saved: %s', $filePath), [Sgr::GREEN]), true); |
88
|
|
|
} catch (\Exception $e) { |
89
|
|
|
$this->outputCli( |
90
|
|
|
Ansi::sgr(sprintf('Error saving shipment documents: %s', $e->getMessage()), [Sgr::RED]), |
91
|
|
|
true |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return new CliResponse('', true); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|