HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/teamdemo.sports-crowd.com/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;
    }
}