| 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\AuthenticationToken::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 retrieve(?string $shipmentId = null): 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', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |             $this->handleApiCall($this->jwt, $url, Method::GET, ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } catch (\Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $this->outputLogger->output(Ansi::sgr(\sprintf('Error: %s', $e->getMessage()), [Sgr::RED]), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return new Response(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function downloadDocuments(?string $shipmentId = null): ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->outputLogger->output(Ansi::clear(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->outputLogger->output(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $url = \sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             '%s%s/shipments/%s/documents', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             Config::string('APP_API_URL'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             Config::string('APP_API_VERSION'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $shipmentId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             if (!$shipmentId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 throw new \InvalidArgumentException('Shipment ID is missing.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->handleApiCall($this->jwt, $url, Method::GET, ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $data = \json_decode($this->responseContent, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             if (isset($data['data']['attributes']['fileData']) && isset($data['data']['attributes']['fileName'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $filePath = \sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                     '%svar/tmp/%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     $this->config()->get('app/path/project'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     $data['data']['attributes']['fileName'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     \file_put_contents($filePath, \base64_decode($data['data']['attributes']['fileData'], true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                     $this->outputLogger->output( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                         Ansi::sgr(\sprintf('Shipment documents saved: %s', $filePath), [Sgr::GREEN]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                         true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 } catch (\Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     $this->outputLogger->output( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                         Ansi::sgr(\sprintf('Error saving shipment documents: %s', $e->getMessage()), [Sgr::RED]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                         true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  |