Completed
Push — master ( 00ede7...fe03b3 )
by Mike
04:35
created

ProviderAndDumperAggregator   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 210
Duplicated Lines 12.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 3
dl 26
loc 210
ccs 96
cts 99
cp 0.9697
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A all() 0 4 1
A get() 0 4 1
B dump() 0 26 2
A geocodeQuery() 0 13 1
A reverseQuery() 0 13 1
A getName() 0 4 1
A geocode() 13 13 1
A reverse() 13 13 1
A limit() 0 7 1
A getLimit() 0 4 1
A registerProvider() 0 6 1
A registerProviders() 0 6 1
A using() 0 6 1
A getProviders() 0 4 1
A getProvider() 0 4 1
A registerProvidersFromConfig() 0 6 1
A getProvidersFromConfiguration() 0 15 2
A getArguments() 0 16 3
A getAdapterClass() 0 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Geocoder\Laravel;
2
3
/**
4
 * This file is part of the Geocoder Laravel package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Mike Bronner <[email protected]>
9
 * @license    MIT License
10
 */
11
12
use Geocoder\Dumper\GeoJson;
13
use Geocoder\Dumper\Gpx;
14
use Geocoder\Dumper\Kml;
15
use Geocoder\Dumper\Wkb;
16
use Geocoder\Dumper\Wkt;
17
use Geocoder\Geocoder;
18
use Geocoder\Query\GeocodeQuery;
19
use Geocoder\Query\ReverseQuery;
20
use Geocoder\Laravel\Exceptions\InvalidDumperException;
21
use Geocoder\ProviderAggregator;
22
use Illuminate\Support\Collection;
23
use ReflectionClass;
24
25
/**
26
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
27
 */
28
class ProviderAndDumperAggregator
29
{
30
    protected $aggregator;
31
    protected $results;
32
33 19
    public function __construct()
34
    {
35 19
        $this->aggregator = new ProviderAggregator();
36 19
        $this->results = collect();
37 19
    }
38
39
    /**
40
     * @deprecated Use `get()` instead.
41
     */
42 1
    public function all() : array
43
    {
44 1
        return $this->results->all();
45
    }
46
47 12
    public function get() : Collection
48
    {
49 12
        return $this->results;
50
    }
51
52 2
    public function dump(string $dumper) : Collection
53
    {
54 2
        $dumperClasses = collect([
55 2
            'geojson' => GeoJson::class,
56
            'gpx' => Gpx::class,
57
            'kml' => Kml::class,
58
            'wkb' => Wkb::class,
59
            'wkt' => Wkt::class,
60
        ]);
61
62 2
        if (!$dumperClasses->has($dumper)) {
63 1
            $errorMessage = implode('', [
64 1
                "The dumper specified ('{$dumper}') is invalid. Valid dumpers ",
65 1
                "are: geojson, gpx, kml, wkb, wkt.",
66
            ]);
67 1
            throw new InvalidDumperException($errorMessage);
68
        }
69
70 1
        $dumperClass = $dumperClasses->get($dumper);
71 1
        $dumper = new $dumperClass;
72 1
        $results = collect($this->results->all());
73
74
        return $results->map(function ($result) use ($dumper) {
75 1
            return $dumper->dump($result);
76 1
        });
77
    }
78
79 1
    public function geocodeQuery(GeocodeQuery $query) : self
80
    {
81 1
        $cacheKey = serialize($query);
82 1
        $this->results = app('cache')->remember(
83 1
            "geocoder-{$cacheKey}",
84 1
            config('geocoder.cache-duraction', 0),
85
            function () use ($query) {
86 1
                return collect($this->aggregator->geocodeQuery($query));
87 1
            }
88
        );
89
90 1
        return $this;
91
    }
92
93 1
    public function reverseQuery(ReverseQuery $query) : self
94
    {
95 1
        $cacheKey = serialize($query);
96 1
        $this->results = app('cache')->remember(
97 1
            "geocoder-{$cacheKey}",
98 1
            config('geocoder.cache-duraction', 0),
99
            function () use ($query) {
100 1
                return collect($this->aggregator->reverseQuery($query));
101 1
            }
102
        );
103
104 1
        return $this;
105
    }
106
107 1
    public function getName() : string
108
    {
109 1
        return $this->aggregator->getName();
110
    }
111
112 11 View Code Duplication
    public function geocode(string $value) : self
113
    {
114 11
        $cacheKey = str_slug(strtolower(urlencode($value)));
115 11
        $this->results = app('cache')->remember(
116 11
            "geocoder-{$cacheKey}",
117 11
            config('geocoder.cache-duraction', 0),
118
            function () use ($value) {
119 11
                return collect($this->aggregator->geocode($value));
120 11
            }
121
        );
122
123 11
        return $this;
124
    }
125
126 1 View Code Duplication
    public function reverse(float $latitude, float $longitude) : self
127
    {
128 1
        $cacheKey = str_slug(strtolower(urlencode("{$latitude}-{$longitude}")));
129 1
        $this->results = app('cache')->remember(
130 1
            "geocoder-{$cacheKey}",
131 1
            config('geocoder.cache-duraction', 0),
132
            function () use ($latitude, $longitude) {
133 1
                return collect($this->aggregator->reverse($latitude, $longitude));
134 1
            }
135
        );
136
137 1
        return $this;
138
    }
139
140 1
    public function limit(int $limit) : self
141
    {
142 1
        $this->aggregator = new ProviderAggregator(null, $limit);
0 ignored issues
show
Unused Code introduced by
The call to ProviderAggregator::__construct() has too many arguments starting with $limit.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
143 1
        $this->registerProvidersFromConfig(collect(config('geocoder.providers')));
144
145 1
        return $this;
146
    }
147
148 1
    public function getLimit() : int
149
    {
150 1
        return $this->limit;
0 ignored issues
show
Bug introduced by
The property limit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
151
    }
152
153 1
    public function registerProvider($provider) : self
154
    {
155 1
        $this->aggregator->registerProvider($provider);
156
157 1
        return $this;
158
    }
159
160 19
    public function registerProviders(array $providers = []) : self
161
    {
162 19
        $this->aggregator->registerProviders($providers);
163
164 19
        return $this;
165
    }
166
167 4
    public function using(string $name) : self
168
    {
169 4
        $this->aggregator = $this->aggregator->using($name);
170
171 4
        return $this;
172
    }
173
174 1
    public function getProviders() : Collection
175
    {
176 1
        return collect($this->aggregator->getProviders());
177
    }
178
179
    protected function getProvider()
180
    {
181
        return $this->aggregator->getProvider();
182
    }
183
184 19
    public function registerProvidersFromConfig(Collection $providers) : self
185
    {
186 19
        $this->registerProviders($this->getProvidersFromConfiguration($providers));
187
188 19
        return $this;
189
    }
190
191
    protected function getProvidersFromConfiguration(Collection $providers) : array
192
    {
193 19
        $providers = $providers->map(function ($arguments, $provider) {
194 19
            $arguments = $this->getArguments($arguments, $provider);
195 19
            $reflection = new ReflectionClass($provider);
196
197 19
            if ($provider === 'Geocoder\Provider\Chain\Chain') {
198 19
                return $reflection->newInstance($arguments);
199
            }
200
201 19
            return $reflection->newInstanceArgs($arguments);
202 19
        });
203
204 19
        return $providers->toArray();
205
    }
206
207 19
    protected function getArguments(array $arguments, string $provider) : array
208
    {
209 19
        if ($provider === 'Geocoder\Provider\Chain\Chain') {
210 19
            return $this->getProvidersFromConfiguration(
211 19
                collect(config('geocoder.providers.Geocoder\Provider\Chain\Chain'))
212
            );
213
        }
214
215 19
        $adapter = $this->getAdapterClass($provider);
216
217 19
        if ($adapter) {
218 19
            array_unshift($arguments, (new $adapter));
219
        }
220
221 19
        return $arguments;
222
    }
223
224 19
    protected function getAdapterClass(string $provider) : string
225
    {
226 19
        $specificAdapters = collect([
227 19
            'Geocoder\Provider\GeoIP2' => 'Geocoder\Adapter\GeoIP2Adapter',
228
            'Geocoder\Provider\MaxMindBinary' => null,
229
        ]);
230
231 19
        if ($specificAdapters->has($provider)) {
232
            return $specificAdapters->get($provider);
233
        }
234
235 19
        return config('geocoder.adapter');
236
    }
237
}
238