File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/app/Core/Address/Application/AddressService.php
<?php
declare(strict_types=1);
namespace App\Core\Address\Application;
use App\Address;
use App\Models\Divipola;
class AddressService
{
public function find(int $id)
{
return Address::findOrFail($id);
}
public function geocodeAddress($address)
{
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://maps.googleapis.com',
'defaults' => [
'exceptions' => false,
],
]);
$responseGuzzle = $client->get('/maps/api/geocode/json?key=' . config('googlmapper.key') . '&address=' . rawurlencode($address->direction), [
'headers' => [
'Accept' => 'application/json',
],
]);
$body = (string) $responseGuzzle->getBody();
$body = json_decode($body);
return $body->results;
}
public function geocodeAddressByCoordinates($latitude, $longitude)
{
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://maps.googleapis.com',
'defaults' => [
'exceptions' => false,
],
]);
$responseGuzzle = $client->get('/maps/api/geocode/json?key=' . config('googlmapper.key') . '&latlng=' . $latitude . ',' . $longitude, [
'headers' => [
'Accept' => 'application/json',
],
]);
$body = (string) $responseGuzzle->getBody();
$body = json_decode($body);
return $body->results;
}
public function getDivipolaInfo($address)
{
if (is_null($address->lat) || is_null($address->long)) {
$this->storeGeocode($address);
}
$gmapsData = $this->geocodeAddressByCoordinates($address->lat, $address->long);
return $this->getDivipolaByGmapsComponents($gmapsData);
}
public function getDivipolaByGmapsComponents($gmapsData)
{
$divipola = null;
$filter = [];
foreach ($gmapsData as $gmaps) {
$addressComponents = $gmaps->address_components;
$country = array_filter($addressComponents, function ($v) {
return in_array('country',$v->types);
});
$filter['country'] = array_pop($country)->long_name;
$level1 = array_filter($addressComponents, function ($v) {
return in_array('administrative_area_level_1',$v->types);
});
$filter['administrative_area_level_1'] = array_pop($level1)->long_name ?: null;
$level2 = array_filter($addressComponents, function ($v) {
return in_array('administrative_area_level_2',$v->types);
});
$locality = array_filter($addressComponents, function ($v) {
return in_array('locality',$v->types);
});
if (count($level2) == 0 && count($locality) == 0){
continue;
}
$filter['administrative_area_level_2'] = array_pop($level2)->long_name ?? array_pop($locality)->long_name;
$divipola = Divipola::where($filter)->first();
if ($divipola) {
break;
}
}
return $divipola;
}
public function storeGeocode($address)
{
$geocodeAddress = $this->geocodeAddress($address);
$firstResult = array_pop($geocodeAddress);
$address->lat = $firstResult->geometry->location->lat;
$address->long = $firstResult->geometry->location->lng;
$address->save();
}
public function create(
int $user_id,
string $direction,
float $lat,
float $long,
string $tag
): Address
{
$address = new Address();
$address->user_id = $user_id;
$address->direction = $direction;
$address->lat = $lat;
$address->long = $long;
$address->tag = $tag;
$address->active = true;
$address->save();
return $address;
}
}