| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Ivory Google Map package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Eric GELOEN <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please read the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Ivory\GoogleMap\Service\Geocoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Http\Client\HttpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Http\Message\MessageFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Ivory\GoogleMap\Base\Bound; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Ivory\GoogleMap\Base\Coordinate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Ivory\GoogleMap\Service\AbstractService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Ivory\GoogleMap\Service\Geocoder\Request\GeocoderRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderAddress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderGeometry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Ivory\GoogleMap\Service\Utility\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @author GeLo <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class GeocoderService extends AbstractService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param HttpClient     $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param MessageFactory $messageFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param Parser|null    $parser | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct(HttpClient $client, MessageFactory $messageFactory, Parser $parser = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/geocode', $parser); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param GeocoderRequestInterface $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return GeocoderResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 46 |  | View Code Duplication |     public function geocode(GeocoderRequestInterface $request) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $response = $this->getClient()->sendRequest($this->createRequest($request->build())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $data = $this->parse((string) $response->getBody(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             'pluralization_rules' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 'address_component' => 'address_components', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 'type'              => 'types', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 'result'            => 'results', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return $this->buildResponse($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param mixed[] $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return GeocoderResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 65 |  | View Code Duplication |     private function buildResponse(array $data) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $response = new GeocoderResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $response->setStatus($data['status']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $response->setResults($this->buildResults($data['results'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param mixed[] $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return GeocoderResult[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     private function buildResults(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $results = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         foreach ($data as $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $results[] = $this->buildResult($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $results; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param mixed[] $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return GeocoderResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     private function buildResult(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $result = new GeocoderResult(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $result->setAddresses($this->buildAddresses($data['address_components'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $result->setGeometry($this->buildGeometry($data['geometry'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $result->setPlaceId($data['place_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $result->setFormattedAddress($data['formatted_address']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         if (isset($data['types'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $result->setTypes($data['types']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (isset($data['partial_match'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $result->setPartialMatch($data['partial_match']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param mixed[] $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @return GeocoderAddress[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     private function buildAddresses(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $addresses = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         foreach ($data as $address) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $addresses[] = $this->buildAddress($address); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return $addresses; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param mixed[] $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return GeocoderAddress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     private function buildAddress(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $address = new GeocoderAddress(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $address->setLongName($data['long_name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $address->setShortName($data['short_name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $address->setTypes($data['types']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return $address; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param mixed[] $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @return GeocoderGeometry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     private function buildGeometry(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $geometry = new GeocoderGeometry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $geometry->setLocation($this->buildCoordinate($data['location'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $geometry->setViewport($this->buildBound($data['viewport'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $geometry->setLocationType($data['location_type']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         if (isset($data['bounds'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             $geometry->setBound($this->buildBound($data['bounds'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return $geometry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @param mixed[] $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @return Bound | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     private function buildBound(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         return new Bound( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             $this->buildCoordinate($data['southwest']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             $this->buildCoordinate($data['northeast']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param mixed[] $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @return Coordinate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     private function buildCoordinate(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         return new Coordinate($data['lat'], $data['lng']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 186 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 187 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.