1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ParcelValue\ApiClient\Domain\Shipments; |
6
|
|
|
|
7
|
|
|
use WebServCo\Api\JsonApi\Document; |
8
|
|
|
use WebServCo\Framework\Cli\Ansi; |
9
|
|
|
use WebServCo\Framework\Cli\Response; |
10
|
|
|
use WebServCo\Framework\Cli\Sgr; |
11
|
|
|
use WebServCo\Framework\Environment\Config; |
12
|
|
|
use WebServCo\Framework\Http\Method; |
13
|
|
|
use WebServCo\Framework\Interfaces\ResponseInterface; |
14
|
|
|
|
15
|
|
|
final class Command extends \ParcelValue\ApiClient\AbstractController |
16
|
|
|
{ |
17
|
|
|
use \ParcelValue\ApiClient\Traits\ControllerApiTrait; |
18
|
|
|
|
19
|
|
|
protected string $jwt; |
20
|
|
|
|
21
|
|
|
protected \WebServCo\Framework\Interfaces\OutputLoggerInterface $outputLogger; |
22
|
|
|
|
23
|
|
|
protected Repository $repository; |
24
|
|
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
parent::__construct(); |
28
|
|
|
|
29
|
|
|
$this->repository = new Repository($this->outputLoader); |
30
|
|
|
|
31
|
|
|
$this->jwt = \ParcelValue\Api\JWT\Helper::generate( |
32
|
|
|
Config::string('APP_API_CLIENT_ID'), |
33
|
|
|
Config::string('APP_API_CLIENT_KEY'), |
34
|
|
|
Config::string('APP_API_SERVER_KEY'), |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$this->outputLogger = new \WebServCo\Framework\Log\CliOutputLogger(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function create(): ResponseInterface |
41
|
|
|
{ |
42
|
|
|
$this->init(); |
43
|
|
|
|
44
|
|
|
$this->outputLogger->output(Ansi::clear(), true); |
45
|
|
|
$this->outputLogger->output(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
46
|
|
|
|
47
|
|
|
$url = \sprintf('%s%s/shipments', Config::string('APP_API_URL'), Config::string('APP_API_VERSION')); |
48
|
|
|
|
49
|
|
|
$shipment = $this->repository->getShipment(); |
50
|
|
|
$document = new Document(); |
51
|
|
|
$document->setData($shipment); |
52
|
|
|
|
53
|
|
|
$this->handleApiCall($this->jwt, $url, Method::POST, $document->toJson()); |
54
|
|
|
|
55
|
|
|
return new Response(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function downloadDocuments(string $shipmentId): ResponseInterface |
59
|
|
|
{ |
60
|
|
|
$this->init(); |
61
|
|
|
|
62
|
|
|
$this->outputLogger->output(Ansi::clear(), true); |
63
|
|
|
$this->outputLogger->output(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
64
|
|
|
|
65
|
|
|
$url = \sprintf( |
66
|
|
|
'%s%s/shipments/%s/documents', |
67
|
|
|
Config::string('APP_API_URL'), |
68
|
|
|
Config::string('APP_API_VERSION'), |
69
|
|
|
$shipmentId, |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
if (!$shipmentId) { |
74
|
|
|
throw new \InvalidArgumentException('Shipment ID is missing.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->handleApiCall($this->jwt, $url, Method::GET, ''); |
78
|
|
|
|
79
|
|
|
$data = \json_decode($this->responseContent, true); |
80
|
|
|
if (isset($data['data']['attributes']['fileData']) && isset($data['data']['attributes']['fileName'])) { |
81
|
|
|
$filePath = \sprintf( |
82
|
|
|
'%svar/tmp/%s', |
83
|
|
|
Config::string('APP_PATH_PROJECT'), |
84
|
|
|
$data['data']['attributes']['fileName'], |
85
|
|
|
); |
86
|
|
|
try { |
87
|
|
|
\file_put_contents($filePath, \base64_decode($data['data']['attributes']['fileData'], true)); |
88
|
|
|
$this->outputLogger->output( |
89
|
|
|
Ansi::sgr(\sprintf('Shipment documents saved: %s', $filePath), [Sgr::GREEN]), |
90
|
|
|
true, |
91
|
|
|
); |
92
|
|
|
} catch (\Throwable $e) { |
93
|
|
|
$this->outputLogger->output( |
94
|
|
|
Ansi::sgr(\sprintf('Error saving shipment documents: %s', $e->getMessage()), [Sgr::RED]), |
95
|
|
|
true, |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} catch (\Throwable $e) { |
100
|
|
|
$this->outputLogger->output(Ansi::sgr(\sprintf('Error: %s', $e->getMessage()), [Sgr::RED]), true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return new Response(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getTrackingInfo(string $shipmentId): ResponseInterface |
107
|
|
|
{ |
108
|
|
|
$this->init(); |
109
|
|
|
|
110
|
|
|
$this->outputLogger->output(Ansi::clear(), true); |
111
|
|
|
$this->outputLogger->output(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
112
|
|
|
|
113
|
|
|
$url = \sprintf( |
114
|
|
|
'%s%s/shipments/%s/tracking', |
115
|
|
|
Config::string('APP_API_URL'), |
116
|
|
|
Config::string('APP_API_VERSION'), |
117
|
|
|
$shipmentId, |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
try { |
121
|
|
|
if (!$shipmentId) { |
122
|
|
|
throw new \InvalidArgumentException('Shipment ID is missing.'); |
123
|
|
|
} |
124
|
|
|
$this->handleApiCall($this->jwt, $url, Method::GET, ''); |
125
|
|
|
} catch (\Throwable $e) { |
126
|
|
|
$this->outputLogger->output(Ansi::sgr(\sprintf('Error: %s', $e->getMessage()), [Sgr::RED]), true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return new Response(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function retrieve(string $shipmentId): ResponseInterface |
133
|
|
|
{ |
134
|
|
|
$this->init(); |
135
|
|
|
|
136
|
|
|
$this->outputLogger->output(Ansi::clear(), true); |
137
|
|
|
$this->outputLogger->output(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
138
|
|
|
|
139
|
|
|
$url = \sprintf( |
140
|
|
|
'%s%s/shipments/%s', |
141
|
|
|
Config::string('APP_API_URL'), |
142
|
|
|
Config::string('APP_API_VERSION'), |
143
|
|
|
$shipmentId, |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
try { |
147
|
|
|
if (!$shipmentId) { |
148
|
|
|
throw new \InvalidArgumentException('Shipment ID is missing.'); |
149
|
|
|
} |
150
|
|
|
$this->handleApiCall($this->jwt, $url, Method::GET, ''); |
151
|
|
|
} catch (\Throwable $e) { |
152
|
|
|
$this->outputLogger->output(Ansi::sgr(\sprintf('Error: %s', $e->getMessage()), [Sgr::RED]), true); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return new Response(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|