Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .idea/phpunit.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 8 additions & 16 deletions laravel-google-maps-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,19 @@ You can install the package via composer:
composer require abenevaut/laravel-google-maps
```

Add GoogleMaps service to your `config/services.php` file:

```php
'googlemaps' => [
'baseUrl' => env('GOOGLE_MAPS_URL', 'https://places.googleapis.com'),
'api_key' => env('GOOGLE_MAPS_API_KEY'),
'debug' => env('GOOGLE_MAPS_DEBUG', false),
],
```
@TODO: explain how to install and use `config/google-maps.php`

## Usage

```php
use abenevaut\GoogleMaps\Facades\GoogleMaps;

// Example: Search for nearby places
$results = GoogleMaps::placesNearby([
'location' => '48.8588443,2.2943506', // Eiffel Tower
'radius' => 1500,
'type' => 'restaurant',
]);

// ... to be continued
$results = GoogleMaps::service('places')
->getClient()
->placesNearby([
'location' => '48.8588443,2.2943506', // Eiffel Tower
'radius' => 1500,
'type' => 'restaurant',
]);
```
11 changes: 11 additions & 0 deletions laravel-google-maps-client/config/google-maps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [
'api_key' => env('GOOGLE_MAPS_API_KEY'),
'places' => [
'debug' => env('APP_DEBUG', false),
],
'timezones' => [
'debug' => env('APP_DEBUG', false),
],
];
4 changes: 0 additions & 4 deletions laravel-google-maps-client/docs/Contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@
## Build

Maintain composer packages with php 8.3

```bash
docker-compose up -d
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
use abenevaut\Infrastructure\Client\AccessTokenInterface;
use Illuminate\Http\Client\PendingRequest;

final class GoogleMapsClient extends AuthenticatedClientAbstract
final class PlacesClient extends AuthenticatedClientAbstract
{
public function __construct(
string $baseUrl,
AccessTokenInterface $accessToken,
bool $debug = false
) {
parent::__construct($baseUrl, $accessToken, $debug);
parent::__construct(
'https://places.googleapis.com',
$accessToken,
$debug
);
}

public function searchNearby(array $requestBody, array $fieldMaskParts = []): array
Expand Down
40 changes: 40 additions & 0 deletions laravel-google-maps-client/src/Client/TimezonesClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace abenevaut\GoogleMaps\Client;

use abenevaut\Infrastructure\Client\AuthenticatedClientAbstract;
use abenevaut\Infrastructure\Client\AccessTokenInterface;

final class TimezonesClient extends AuthenticatedClientAbstract
{
public function __construct(
AccessTokenInterface $accessToken,
bool $debug = false
) {
parent::__construct(
'https://maps.googleapis.com/maps/api/timezone',
$accessToken,
$debug
);
}

/**
* https://developers.google.com/maps/documentation/timezone/requests-timezone?hl=fr
*
* @return array
* @throws \Illuminate\Http\Client\ConnectionException
* @throws \Illuminate\Http\Client\RequestException
*/
public function searchTimezone(float $lat, float $lng, $timestamp): array
{
return $this
->request()
->get('/json', [
'location' => "{$lat},{$lng}",
'timestamp' => $timestamp,
'key' => $this->accessToken->getAccessToken(),
])
->throw()
->json();
}
}
4 changes: 2 additions & 2 deletions laravel-google-maps-client/src/Facades/GoogleMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace abenevaut\GoogleMaps\Facades;

use abenevaut\GoogleMaps\Client\GoogleMapsClient as GoogleMapsClient;
use abenevaut\GoogleMaps\Services\GoogleMapsServiceFactory;
use Illuminate\Support\Facades\Facade;

class GoogleMaps extends Facade
{
protected static function getFacadeAccessor()
{
return GoogleMapsClient::class;
return GoogleMapsServiceFactory::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace abenevaut\GoogleMaps\Infrastructure;

use abenevaut\Infrastructure\Client\ClientAbstract;

interface GoogleMapsServiceInterface
{
public function getClient(): ClientAbstract;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace abenevaut\GoogleMaps\Providers;

use abenevaut\GoogleMaps\Client\AccessToken;
use abenevaut\GoogleMaps\Client\GoogleMapsClient;
use abenevaut\GoogleMaps\Services\GoogleMapsService;
use abenevaut\GoogleMaps\Client\PlacesClient;
use abenevaut\GoogleMaps\Services\GoogleMapsServiceFactory;
use abenevaut\GoogleMaps\Services\PlacesService;
use abenevaut\GoogleMaps\Services\TimezonesService;
use Illuminate\Container\Container;
use Illuminate\Support\ServiceProvider;

Expand All @@ -16,28 +18,48 @@ public function register()
{
parent::register();

// Registers the GoogleMaps client instance with the API key provided via AccessToken
$this->app->singleton(GoogleMapsClient::class, function (Container $app) {
$driversList = $this->app->get('config')->get('google-maps')->keys();

$this->app->singleton(GoogleMapsServiceFactory::class, function (Container $app, $driversList) {
// @codeCoverageIgnoreStart
$accessToken = new AccessToken(
$app->get('config')->get('services.googlemaps.api_key')
return new GoogleMapsServiceFactory(
[$app, 'resolve'],
$driversList
);
return new GoogleMapsClient(
$app->get('config')->get('services.googlemaps.baseUrl'),
$accessToken,
$app->get('config')->get('services.googlemaps.debug', false),
// @codeCoverageIgnoreEnd
});

$this->app->singleton(AccessToken::class, function (Container $app) {
// @codeCoverageIgnoreStart
return new AccessToken(
$app->get('config')->get('google-maps.api_key')
);
// @codeCoverageIgnoreEnd
});

$this->app->singleton(PlacesService::class, function (Container $app) {
// @codeCoverageIgnoreStart
return new PlacesService(
$app->make(AccessToken::class),
$app->get('config')->get('google-maps.places.debug', false),
);
// @codeCoverageIgnoreEnd
});

$this->app->alias(GoogleMapsClient::class, 'googlemaps.http-client.authenticated');
$this->app->singleton(TimezonesService::class, function (Container $app) {
// @codeCoverageIgnoreStart
return new TimezonesService(
$app->make(AccessToken::class),
$app->get('config')->get('google-maps.timezone.debug', false),
);
// @codeCoverageIgnoreEnd
});
}

public function provides()
{
return [
GoogleMapsClient::class,
'googlemaps.http-client.authenticated',
GoogleMapsServiceFactory::class,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace abenevaut\GoogleMaps\Services;

final class GoogleMapsServiceFactory
{
public function __construct(
protected readonly array $applicationResolver,
private readonly array $services
) {
}

public function services(): array
{
return $this->services;
}

public function service(string $driver): object
{
if (!in_array($driver, $this->services())) {
throw new \RuntimeException("Driver doesn't exist.");
}

$driver = ucfirst($driver);

return $this->resolve("{$driver}Service");
}

public function classResolution(string $driver): string
{
if (!in_array($driver, $this->services())) {
throw new \RuntimeException("Driver doesn't exist.");
}

$driver = ucfirst($driver);

return "\abenevaut\GoogleMaps\Services\{$driver}Service";
}

protected function resolve(string $driver): object
{
return call_user_func($this->applicationResolver, $driver);
}
}
24 changes: 24 additions & 0 deletions laravel-google-maps-client/src/Services/PlacesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace abenevaut\GoogleMaps\Services;

use abenevaut\GoogleMaps\Client\PlacesClient;
use abenevaut\GoogleMaps\Infrastructure\GoogleMapsServiceInterface;
use abenevaut\Infrastructure\Client\{AccessTokenInterface, ClientAbstract};

final class PlacesService implements GoogleMapsServiceInterface
{
protected PlacesClient $client;

public function __construct(
AccessTokenInterface $accessToken,
bool $debug = false
) {
$this->client = new PlacesClient($accessToken, $debug);
}

public function getClient(): ClientAbstract
{
return $this->client;
}
}
24 changes: 24 additions & 0 deletions laravel-google-maps-client/src/Services/TimezonesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace abenevaut\GoogleMaps\Services;

use abenevaut\GoogleMaps\Client\TimezonesClient;
use abenevaut\GoogleMaps\Infrastructure\GoogleMapsServiceInterface;
use abenevaut\Infrastructure\Client\{AccessTokenInterface, ClientAbstract};

final class TimezonesService implements GoogleMapsServiceInterface
{
protected TimezonesClient $client;

public function __construct(
AccessTokenInterface $accessToken,
bool $debug = false
) {
$this->client = new TimezonesClient($accessToken, $debug);
}

public function getClient(): ClientAbstract
{
return $this->client;
}
}