| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace ParcelValue\ApiClient\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use WebServCo\Framework\Cli\Ansi; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use WebServCo\Framework\Cli\Sgr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use WebServCo\Framework\Http\Method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use WebServCo\Framework\Interfaces\ResponseInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | trait ControllerApiTrait | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     protected ResponseInterface $httpResponse; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     protected int $responseStatus; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     protected string $responseContent; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     protected \WebServCo\Framework\Interfaces\OutputLoggerInterface $outputLogger; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * Returns data if exists, $defaultValue otherwise. | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @param mixed $defaultValue | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     abstract public function data(string $key, $defaultValue = false); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     abstract protected function request(): \WebServCo\Framework\Interfaces\RequestInterface; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     * @param array<string,mixed>|string $requestData | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected function handleApiCall(string $jwt, string $url, string $method, $requestData): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->outputLogger->output('', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->outputLogger->output(\sprintf('REQUEST: %s %s', $method, $url), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $logger = new \WebServCo\Framework\Log\FileLogger( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'ParcelValueAPI', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             \sprintf('%svar/log/', $this->data('path/project', '')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $apiHelper = new \ParcelValue\Api\Helper($logger, $jwt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->httpResponse = $apiHelper->getResponse($url, $method, $requestData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (Method::POST === $method) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $this->outputLogger->output('', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $this->outputLogger->output(\var_export($requestData, true), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->responseStatus = $this->httpResponse->getStatus(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->responseContent = $this->httpResponse->getContent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->outputLogger->output('', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $statusCodes = \WebServCo\Framework\Http\StatusCode::getSupported(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->outputLogger->output( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             \sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 'RESPONSE: %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 Ansi::sgr( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     \sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                         '%s %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                         $this->responseStatus, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                         $statusCodes[$this->responseStatus] ?? '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     [400 > $this->responseStatus ? Sgr::GREEN : Sgr::RED], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->outputLogger->output('', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->outputLogger->output($this->responseContent, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->outputLogger->output('', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->outputLogger->output( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             \sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 'Processed result: %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 \json_encode(\json_decode($this->responseContent, true), \JSON_PRETTY_PRINT), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  |