| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Speicher210\KontaktIO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\Exception\ClientException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use JMS\Serializer\SerializerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Speicher210\KontaktIO\Exception\ApiException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Speicher210\KontaktIO\Exception\ApiKeyInvalidException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Speicher210\KontaktIO\Model\ApiErrorResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Abstract resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class AbstractResource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * The API client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Serializer interface to serialize / deserialize the request / responses. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var SerializerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param Client $client The API client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param SerializerInterface $serializer Serializer interface to serialize / deserialize the request / responses. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct(Client $client, SerializerInterface $serializer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->serializer = $serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Create an ApiException from a client exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param ClientException $e The client exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return ApiException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |     protected function createApiException(ClientException $e) | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         $response = $e->getResponse(); | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         if ($response->getStatusCode() === 401 || $response->getStatusCode() === 403) { | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |             throw new ApiKeyInvalidException($response); | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         if ($response->getBody()->getSize() > 0) { | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |             /** @var ApiErrorResponse $apiErrorResponse */ | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |             $apiErrorResponse = $this->serializer->deserialize( | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |                 $e->getResponse()->getBody(), | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |                 ApiErrorResponse::class, | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |                 'json' | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             ); | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             $apiErrorResponse = new ApiErrorResponse(); | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |             $apiErrorResponse->setStatus($response->getStatusCode()); | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |             $apiErrorResponse->setMessage($response->getReasonPhrase()); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         return new ApiException($apiErrorResponse, $e); | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |