Skip to content

Commit 1b25952

Browse files
committed
Refactor and clean up the service provider
1 parent 4926302 commit 1b25952

File tree

1 file changed

+39
-61
lines changed

1 file changed

+39
-61
lines changed

src/GeocoderServiceProvider.php

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace Toin0u\Geocoder;
22

33
/**
44
* This file is part of the GeocoderLaravel library.
@@ -9,19 +9,12 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Toin0u\Geocoder;
13-
1412
use Geocoder\ProviderAggregator;
1513
use Geocoder\Provider\Chain;
1614
use Illuminate\Support\Collection;
1715
use Illuminate\Support\ServiceProvider;
1816
use ReflectionClass;
1917

20-
/**
21-
* Geocoder service provider
22-
*
23-
* @author Antoine Corcy <contact@sbin.dk>
24-
*/
2518
class GeocoderServiceProvider extends ServiceProvider
2619
{
2720
/**
@@ -32,9 +25,7 @@ class GeocoderServiceProvider extends ServiceProvider
3225
public function boot()
3326
{
3427
$source = realpath(__DIR__ . '/../config/geocoder.php');
35-
3628
$this->publishes([$source => config_path('geocoder.php')], 'config');
37-
3829
$this->mergeConfigFrom($source, 'geocoder');
3930
}
4031

@@ -45,13 +36,7 @@ public function boot()
4536
*/
4637
public function register()
4738
{
48-
$this->app->singleton('geocoder.adapter', function ($app) {
49-
$adapter = config('geocoder.adapter');
50-
51-
return new $adapter;
52-
});
53-
54-
$this->app->singleton('geocoder', function ($app) {
39+
$this->app->singleton('geocoder', function () {
5540
$geocoder = new ProviderAggregator();
5641
$geocoder->registerProviders(
5742
$this->getProviders(collect(config('geocoder.providers')))
@@ -61,10 +46,16 @@ public function register()
6146
});
6247
}
6348

49+
/**
50+
* Instantiate the configured Providers, as well as the Chain Provider.
51+
*
52+
* @param Collection
53+
* @return array
54+
*/
6455
private function getProviders(Collection $providers)
6556
{
6657
$providers = $providers->map(function ($arguments, $provider) {
67-
$arguments = $this->prepArguments($arguments, $provider);
58+
$arguments = $this->getArguments($arguments, $provider);
6859
$reflection = new ReflectionClass($provider);
6960

7061
if ($provider === 'Geocoder\Provider\Chain') {
@@ -77,64 +68,51 @@ private function getProviders(Collection $providers)
7768
return $providers->toArray();
7869
}
7970

80-
private function prepArguments(array $arguments, $provider)
71+
/**
72+
* Insert the required Adapter instance (if required) as the first element
73+
* of the arguments array.
74+
*
75+
* @param array
76+
* @param string
77+
* @return string
78+
*/
79+
private function getArguments(array $arguments, $provider)
8180
{
82-
$specificAdapter = $this->providerRequiresSpecificAdapter($provider);
83-
84-
if ($specificAdapter) {
85-
array_unshift($arguments, $specificAdapter);
86-
87-
return $arguments;
88-
}
89-
90-
if ($this->providerRequiresAdapter($provider)) {
91-
array_unshift($arguments, app('geocoder.adapter'));
92-
93-
return $arguments;
94-
}
95-
9681
if ($provider === 'Geocoder\Provider\Chain') {
9782
return $this->getProviders(
9883
collect(config('geocoder.providers.Geocoder\Provider\Chain'))
9984
);
10085
}
10186

87+
$adapter = $this->getAdapterClass($provider);
88+
89+
if ($adapter) {
90+
array_unshift($arguments, (new $adapter));
91+
}
92+
10293
return $arguments;
10394
}
10495

105-
private function providerRequiresSpecificAdapter($provider)
96+
/**
97+
* Get the required Adapter class name for the current provider. It will
98+
* select a specific adapter if required, handle the Chain provider, and
99+
* return the default configured adapter if non of the above are true.
100+
*
101+
* @param string
102+
* @return string
103+
*/
104+
private function getAdapterClass($provider)
106105
{
107106
$specificAdapters = collect([
108107
'Geocoder\Provider\GeoIP2' => 'Geocoder\Adapter\GeoIP2Adapter',
108+
'Geocoder\Provider\MaxMindBinary' => null,
109109
]);
110110

111-
return $specificAdapters->get($provider);
112-
}
113-
114-
private function providerRequiresAdapter($provider)
115-
{
116-
$providersRequiringAdapter = collect([
117-
'Geocoder\Provider\ArcGISOnline',
118-
'Geocoder\Provider\BingMaps',
119-
'Geocoder\Provider\FreeGeoIp',
120-
'Geocoder\Provider\GeoIPs',
121-
'Geocoder\Provider\Geonames',
122-
'Geocoder\Provider\GeoPlugin',
123-
'Geocoder\Provider\GoogleMaps',
124-
'Geocoder\Provider\GoogleMapsBusiness',
125-
'Geocoder\Provider\HostIp',
126-
'Geocoder\Provider\IpInfoDb',
127-
'Geocoder\Provider\MapQuest',
128-
'Geocoder\Provider\MaxMind',
129-
'Geocoder\Provider\Nominatim',
130-
'Geocoder\Provider\OpenCage',
131-
'Geocoder\Provider\OpenStreetMap',
132-
'Geocoder\Provider\Provider',
133-
'Geocoder\Provider\TomTom',
134-
'Geocoder\Provider\Yandex',
135-
]);
111+
if ($specificAdapters->has($provider)) {
112+
return $specificAdapters->get($provider);
113+
}
136114

137-
return $providersRequiringAdapter->contains($provider);
115+
return config('geocoder.adapter');
138116
}
139117

140118
/**
@@ -144,6 +122,6 @@ private function providerRequiresAdapter($provider)
144122
*/
145123
public function provides()
146124
{
147-
return ['geocoder', 'geocoder.adapter', 'geocoder.chain'];
125+
return ['geocoder'];
148126
}
149127
}

0 commit comments

Comments
 (0)