Completed
Push — master ( b424b3...7607ec )
by Mike
07:41
created

ProviderAndDumperAggregator::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 $limit;
32
    protected $results;
33
34 23
    public function __construct()
35
    {
36 23
        $this->aggregator = new ProviderAggregator();
37 23
        $this->results = collect();
38 23
    }
39
40
    /**
41
     * @deprecated Use `get()` instead.
42
     */
43 1
    public function all() : array
44
    {
45 1
        return $this->results->all();
46
    }
47
48 16
    public function get() : Collection
49
    {
50 16
        return $this->results;
51
    }
52
53 2
    public function dump(string $dumper) : Collection
54
    {
55 2
        $dumperClasses = collect([
56 2
            'geojson' => GeoJson::class,
57
            'gpx' => Gpx::class,
58
            'kml' => Kml::class,
59
            'wkb' => Wkb::class,
60
            'wkt' => Wkt::class,
61
        ]);
62
63 2
        if (!$dumperClasses->has($dumper)) {
64 1
            $errorMessage = implode('', [
65 1
                "The dumper specified ('{$dumper}') is invalid. Valid dumpers ",
66 1
                "are: geojson, gpx, kml, wkb, wkt.",
67
            ]);
68 1
            throw new InvalidDumperException($errorMessage);
69
        }
70
71 1
        $dumperClass = $dumperClasses->get($dumper);
72 1
        $dumper = new $dumperClass;
73 1
        $results = collect($this->results->all());
74
75 1
        return $results->map(function ($result) use ($dumper) {
76 1
            return $dumper->dump($result);
77 1
        });
78
    }
79
80 1
    public function geocodeQuery(GeocodeQuery $query) : self
81
    {
82 1
        $cacheKey = serialize($query);
83 1
        $this->results = app('cache')->remember(
84 1
            "geocoder-{$cacheKey}",
85 1
            config('geocoder.cache-duration', 0),
86 1
            function () use ($query) {
87 1
                return collect($this->aggregator->geocodeQuery($query));
88 1
            }
89
        );
90
91 1
        return $this;
92
    }
93
94 1
    public function reverseQuery(ReverseQuery $query) : self
95
    {
96 1
        $cacheKey = serialize($query);
97 1
        $this->results = app('cache')->remember(
98 1
            "geocoder-{$cacheKey}",
99 1
            config('geocoder.cache-duration', 0),
100 1
            function () use ($query) {
101 1
                return collect($this->aggregator->reverseQuery($query));
102 1
            }
103
        );
104
105 1
        return $this;
106
    }
107
108 1
    public function getName() : string
109
    {
110 1
        return $this->aggregator->getName();
111
    }
112
113 15 View Code Duplication
    public function geocode(string $value) : self
114
    {
115 15
        $cacheKey = str_slug(strtolower(urlencode($value)));
116 15
        $this->results = app('cache')->remember(
117 15
            "geocoder-{$cacheKey}",
118 15
            config('geocoder.cache-duration', 0),
119 15
            function () use ($value) {
120 15
                return collect($this->aggregator->geocode($value));
121 15
            }
122
        );
123
124 15
        return $this;
125
    }
126
127 1 View Code Duplication
    public function reverse(float $latitude, float $longitude) : self
128
    {
129 1
        $cacheKey = str_slug(strtolower(urlencode("{$latitude}-{$longitude}")));
130 1
        $this->results = app('cache')->remember(
131 1
            "geocoder-{$cacheKey}",
132 1
            config('geocoder.cache-duration', 0),
133 1
            function () use ($latitude, $longitude) {
134 1
                return collect($this->aggregator->reverse($latitude, $longitude));
135 1
            }
136
        );
137
138 1
        return $this;
139
    }
140
141 1
    public function limit(int $limit) : self
142
    {
143 1
        $this->aggregator = new ProviderAggregator(null, $limit);
144 1
        $this->registerProvidersFromConfig(collect(config('geocoder.providers')));
145 1
        $this->limit = $limit;
146
147 1
        return $this;
148
    }
149
150 1
    public function getLimit() : int
151
    {
152 1
        return $this->limit;
153
    }
154
155 1
    public function registerProvider($provider) : self
156
    {
157 1
        $this->aggregator->registerProvider($provider);
158
159 1
        return $this;
160
    }
161
162 23
    public function registerProviders(array $providers = []) : self
163
    {
164 23
        $this->aggregator->registerProviders($providers);
165
166 23
        return $this;
167
    }
168
169 5
    public function using(string $name) : self
170
    {
171 5
        $this->aggregator = $this->aggregator->using($name);
172
173 5
        return $this;
174
    }
175
176 2
    public function getProviders() : Collection
177
    {
178 2
        return collect($this->aggregator->getProviders());
179
    }
180
181
    /**
182
     * @deprecated Use `getProviders()` instead.
183
     */
184 1
    public function getProvider()
185
    {
186 1
        return $this->getProviders()->first();
187
    }
188
189 23
    public function registerProvidersFromConfig(Collection $providers) : self
190
    {
191 23
        $this->registerProviders($this->getProvidersFromConfiguration($providers));
192
193 23
        return $this;
194
    }
195
196
    protected function getProvidersFromConfiguration(Collection $providers) : array
197
    {
198 23
        $providers = $providers->map(function ($arguments, $provider) {
199 23
            $arguments = $this->getArguments($arguments, $provider);
200 23
            $reflection = new ReflectionClass($provider);
201
202 23
            if ($provider === 'Geocoder\Provider\Chain\Chain') {
203 23
                return $reflection->newInstance($arguments);
204
            }
205
206 23
            return $reflection->newInstanceArgs($arguments);
207 23
        });
208
209 23
        return $providers->toArray();
210
    }
211
212 23
    protected function getArguments(array $arguments, string $provider) : array
213
    {
214 23
        if ($provider === 'Geocoder\Provider\Chain\Chain') {
215 23
            return $this->getProvidersFromConfiguration(
216 23
                collect(config('geocoder.providers.Geocoder\Provider\Chain\Chain'))
217
            );
218
        }
219
220 23
        $adapter = $this->getAdapterClass($provider);
221
222 23
        if ($adapter) {
223 23
            array_unshift($arguments, (new $adapter));
224
        }
225
226 23
        return $arguments;
227
    }
228
229 23
    protected function getAdapterClass(string $provider) : string
230
    {
231 23
        $specificAdapters = collect([
232 23
            'Geocoder\Provider\GeoIP2' => 'Geocoder\Adapter\GeoIP2Adapter',
233
            'Geocoder\Provider\MaxMindBinary' => null,
234
        ]);
235
236 23
        if ($specificAdapters->has($provider)) {
237
            return $specificAdapters->get($provider);
238
        }
239
240 23
        return config('geocoder.adapter');
241
    }
242
}
243