| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Werkspot\KvkApi\Client\Adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GuzzleHttp\ClientInterface as GuzzleClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GuzzleHttp\Exception\RequestException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Werkspot\KvkApi\Client\Adapter\Guzzle\Exception\Handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Werkspot\KvkApi\Client\Authentication\AuthenticationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Werkspot\KvkApi\Client\Endpoint\MapperInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Werkspot\KvkApi\Client\Search\QueryInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | final class Guzzle implements AdapterInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @var GuzzleClientInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     private $guzzleClient; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var AuthenticationInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     private $authentication; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @var MapperInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     private $endpointMapper; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 22 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         GuzzleClientInterface $guzzleClient, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         AuthenticationInterface $authentication, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         MapperInterface $urlMapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 22 |  |         $this->guzzleClient = $guzzleClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 22 |  |         $this->authentication = $authentication; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 22 |  |         $this->endpointMapper = $urlMapper; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 | 22 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 19 |  |     public function getEndpoint(string $endpoint, QueryInterface $searchQuery): ResponseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 19 |  |         return $this->get( | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 19 |  |             $this->endpointMapper->map($endpoint), | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 19 |  |             ['query' => $searchQuery->get()] | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |     public function getUrl(string $url): ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |         return $this->get($url); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 17 |  |     public function getJson(ResponseInterface $response): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 17 |  |         return $response->getBody()->getContents(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 21 |  |     private function get(string $url, ?array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 21 |  |         $options = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 21 |  |             $options, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 21 |  |             ['headers' => [$this->authentication->getHeader()]] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |          ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 21 |  |             return $this->guzzleClient->get($url, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |         } catch (RequestException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |             Handler::handleRequestException($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |