Skip to content

Commit 00ede74

Browse files
committed
Change from using cache helper method to IOC method, bring tests to green
1 parent e1812c9 commit 00ede74

File tree

3 files changed

+71
-74
lines changed

3 files changed

+71
-74
lines changed

src/ProviderAndDumperAggregator.php

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@
2020
use Geocoder\Laravel\Exceptions\InvalidDumperException;
2121
use Geocoder\ProviderAggregator;
2222
use Illuminate\Support\Collection;
23+
use ReflectionClass;
2324

2425
/**
2526
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2627
*/
2728
class ProviderAndDumperAggregator
2829
{
2930
protected $aggregator;
31+
protected $limit;
3032
protected $results;
3133

32-
public function __construct(int $limit = Geocoder::DEFAULT_RESULT_LIMIT)
34+
public function __construct(int $limit = null)
3335
{
3436
$this->aggregator = new ProviderAggregator($limit);
37+
$this->limit = $limit;
3538
$this->results = collect();
3639
}
3740

@@ -78,7 +81,7 @@ public function dump(string $dumper) : Collection
7881
public function geocodeQuery(GeocodeQuery $query) : self
7982
{
8083
$cacheKey = serialize($query);
81-
$this->results = cache()->remember(
84+
$this->results = app('cache')->remember(
8285
"geocoder-{$cacheKey}",
8386
config('geocoder.cache-duraction', 0),
8487
function () use ($query) {
@@ -92,7 +95,7 @@ function () use ($query) {
9295
public function reverseQuery(ReverseQuery $query) : self
9396
{
9497
$cacheKey = serialize($query);
95-
$this->results = cache()->remember(
98+
$this->results = app('cache')->remember(
9699
"geocoder-{$cacheKey}",
97100
config('geocoder.cache-duraction', 0),
98101
function () use ($query) {
@@ -111,7 +114,7 @@ public function getName() : string
111114
public function geocode(string $value) : self
112115
{
113116
$cacheKey = str_slug(strtolower(urlencode($value)));
114-
$this->results = cache()->remember(
117+
$this->results = app('cache')->remember(
115118
"geocoder-{$cacheKey}",
116119
config('geocoder.cache-duraction', 0),
117120
function () use ($value) {
@@ -125,7 +128,7 @@ function () use ($value) {
125128
public function reverse(float $latitude, float $longitude) : self
126129
{
127130
$cacheKey = str_slug(strtolower(urlencode("{$latitude}-{$longitude}")));
128-
$this->results = cache()->remember(
131+
$this->results = app('cache')->remember(
129132
"geocoder-{$cacheKey}",
130133
config('geocoder.cache-duraction', 0),
131134
function () use ($latitude, $longitude) {
@@ -138,14 +141,16 @@ function () use ($latitude, $longitude) {
138141

139142
public function limit(int $limit) : self
140143
{
141-
$this->aggregator->limit($limit);
144+
$this->aggregator = new ProviderAggregator(null, $limit);
145+
$this->registerProvidersFromConfig(collect(config('geocoder.providers')));
146+
$this->limit = $limit;
142147

143148
return $this;
144149
}
145150

146151
public function getLimit() : int
147152
{
148-
return $this->aggregator->getLimit();
153+
return $this->limit;
149154
}
150155

151156
public function registerProvider($provider) : self
@@ -164,7 +169,7 @@ public function registerProviders(array $providers = []) : self
164169

165170
public function using(string $name) : self
166171
{
167-
$this->aggregator->using($name);
172+
$this->aggregator = $this->aggregator->using($name);
168173

169174
return $this;
170175
}
@@ -178,4 +183,58 @@ protected function getProvider()
178183
{
179184
return $this->aggregator->getProvider();
180185
}
186+
187+
public function registerProvidersFromConfig(Collection $providers) : self
188+
{
189+
$this->registerProviders($this->getProvidersFromConfiguration($providers));
190+
191+
return $this;
192+
}
193+
194+
protected function getProvidersFromConfiguration(Collection $providers) : array
195+
{
196+
$providers = $providers->map(function ($arguments, $provider) {
197+
$arguments = $this->getArguments($arguments, $provider);
198+
$reflection = new ReflectionClass($provider);
199+
200+
if ($provider === 'Geocoder\Provider\Chain\Chain') {
201+
return $reflection->newInstance($arguments);
202+
}
203+
204+
return $reflection->newInstanceArgs($arguments);
205+
});
206+
207+
return $providers->toArray();
208+
}
209+
210+
protected function getArguments(array $arguments, string $provider) : array
211+
{
212+
if ($provider === 'Geocoder\Provider\Chain\Chain') {
213+
return $this->getProvidersFromConfiguration(
214+
collect(config('geocoder.providers.Geocoder\Provider\Chain\Chain'))
215+
);
216+
}
217+
218+
$adapter = $this->getAdapterClass($provider);
219+
220+
if ($adapter) {
221+
array_unshift($arguments, (new $adapter));
222+
}
223+
224+
return $arguments;
225+
}
226+
227+
protected function getAdapterClass(string $provider) : string
228+
{
229+
$specificAdapters = collect([
230+
'Geocoder\Provider\GeoIP2' => 'Geocoder\Adapter\GeoIP2Adapter',
231+
'Geocoder\Provider\MaxMindBinary' => null,
232+
]);
233+
234+
if ($specificAdapters->has($provider)) {
235+
return $specificAdapters->get($provider);
236+
}
237+
238+
return config('geocoder.adapter');
239+
}
181240
}

src/Providers/GeocoderService.php

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ public function boot()
2727
$this->publishes([$configPath => config_path('geocoder.php')], 'config');
2828
$this->mergeConfigFrom($configPath, 'geocoder');
2929
$this->app->singleton('geocoder', function () {
30-
return (new ProviderAndDumperAggregator)->registerProviders(
31-
$this->getProviders(collect(config('geocoder.providers')))
32-
);
30+
return (new ProviderAndDumperAggregator)->registerProvidersFromConfig(collect(config('geocoder.providers')));
3331
});
3432
}
3533

@@ -38,65 +36,6 @@ public function register()
3836
$this->app->alias('Geocoder', Geocoder::class);
3937
}
4038

41-
/**
42-
* Instantiate the configured Providers, as well as the Chain Provider.
43-
*/
44-
private function getProviders(Collection $providers) : array
45-
{
46-
$providers = $providers->map(function ($arguments, $provider) {
47-
$arguments = $this->getArguments($arguments, $provider);
48-
$reflection = new ReflectionClass($provider);
49-
50-
if ($provider === 'Geocoder\Provider\Chain\Chain') {
51-
return $reflection->newInstance($arguments);
52-
}
53-
54-
return $reflection->newInstanceArgs($arguments);
55-
});
56-
57-
return $providers->toArray();
58-
}
59-
60-
/**
61-
* Insert the required Adapter instance (if required) as the first element
62-
* of the arguments array.
63-
*/
64-
private function getArguments(array $arguments, string $provider) : array
65-
{
66-
if ($provider === 'Geocoder\Provider\Chain\Chain') {
67-
return $this->getProviders(
68-
collect(config('geocoder.providers.Geocoder\Provider\Chain\Chain'))
69-
);
70-
}
71-
72-
$adapter = $this->getAdapterClass($provider);
73-
74-
if ($adapter) {
75-
array_unshift($arguments, (new $adapter));
76-
}
77-
78-
return $arguments;
79-
}
80-
81-
/**
82-
* Get the required Adapter class name for the current provider. It will
83-
* select a specific adapter if required, handle the Chain provider, and
84-
* return the default configured adapter if non of the above are true.
85-
*/
86-
private function getAdapterClass(string $provider) : string
87-
{
88-
$specificAdapters = collect([
89-
'Geocoder\Provider\GeoIP2' => 'Geocoder\Adapter\GeoIP2Adapter',
90-
'Geocoder\Provider\MaxMindBinary' => null,
91-
]);
92-
93-
if ($specificAdapters->has($provider)) {
94-
return $specificAdapters->get($provider);
95-
}
96-
97-
return config('geocoder.adapter');
98-
}
99-
10039
public function provides() : array
10140
{
10241
return ['geocoder'];

tests/Laravel5_3/Providers/GeocoderServiceTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ public function testCacheIsUsed()
182182
$result = app('geocoder')->geocode('1600 Pennsylvania Ave., Washington, DC USA')
183183
->get();
184184

185-
$this->assertTrue(cache()->has("geocoder-{$cacheKey}"));
186-
$this->assertEquals($result, cache("geocoder-{$cacheKey}"));
185+
$this->assertTrue(app('cache')->has("geocoder-{$cacheKey}"));
186+
$this->assertEquals($result, app('cache')->get("geocoder-{$cacheKey}"));
187187
}
188188

189189
/**
@@ -236,7 +236,6 @@ public function testGetNameReturnsString()
236236
public function testLimitingOfResults()
237237
{
238238
$expectedLimit = 1;
239-
240239
app('geocoder')->limit($expectedLimit);
241240
$actualLimit = app('geocoder')->getLimit();
242241
$results = app('geocoder')->using('chain')
@@ -277,6 +276,6 @@ public function testJapaneseCharacterGeocoding()
277276
->get();
278277

279278
$this->assertEquals($cacheKey, '108-0075e69db1e4baace983bde6b8afe58cbae6b8afe58d97efbc92e4b881e79baeefbc91efbc96efbc8defbc93');
280-
$this->assertTrue(cache()->has("geocoder-{$cacheKey}"));
279+
$this->assertTrue(app('cache')->has("geocoder-{$cacheKey}"));
281280
}
282281
}

0 commit comments

Comments
 (0)