| 1 |  |  | <?php declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Suilven\CovidAPIClient\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Suilven\CovidAPIClient\Model\Results; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Suilven\CovidAPIClient\Transport\Http; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | class APIClient | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     /** @var \Suilven\CovidAPIClient\Transport\Http */ | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     private $http; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         $this->http = new Http(); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @param array<\Suilven\CovidAPIClient\Filter\Filter> $filters | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function getData(array $filters): Results | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $encodedFilters = $this->getFiltersForURL($filters); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $structure = $this->getStructure(); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $url = Http::ENDPOINT . '?filters=' . $encodedFilters; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $url .= '&structure=' . $structure; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         // if no string is returned, due to HTTP errors, an exception will have been trhown | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         /** @var string $gzipped */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $gzipped = $this->http->request($url); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         /** @var string $gunzipped */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $gunzipped = \gzdecode($gzipped); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         return new Results($gunzipped); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     /** @param array<\Suilven\CovidAPIClient\Filter\Filter> $filters */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function getFiltersForURL(array $filters): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $encodedFilters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         /** @var \Suilven\CovidAPIClient\Filter\Filter $filter */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         foreach ($filters as $filter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $encodedFilters[] = 'areaType=' . $filter->getAreaType()->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             if (!\is_null($filter->getAreaCode())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 $encodedFilters[] = 'areaCode=' . $filter->getAreaCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if (!\is_null($filter->getAreaName())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $encodedFilters[] = 'areaName=' . $filter->getAreaName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             if (\is_null($filter->getDate())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $encodedFilters[] = 'date=' . $filter->getDate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |         return \implode(';', $encodedFilters); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     private function getStructure(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $structure = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             'date' => 'date', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'areaName' => 'areaName', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             'areaCode' => 'areaCode', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             'newCasesByPublishDate' => 'newCasesByPublishDate', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             'cumCasesByPublishDate' => 'cumCasesByPublishDate', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'newDeaths28DaysByDeathDate' => 'newDeaths28DaysByDeathDate', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'cumDeaths28DaysByDeathDate' => 'cumDeaths28DaysByDeathDate', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // this value will always be set | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |         return (string) \json_encode($structure); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 84 |  |  |  |