Completed
Push — master ( 5081da...0415ba )
by
unknown
24:28
created

ProviderAndDumperAggregator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 133
Duplicated Lines 19.55 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 26
loc 133
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A reverse() 13 13 1
A __construct() 0 4 1
A all() 0 4 1
A get() 0 4 1
B dump() 0 26 2
A geocodeQuery() 0 4 1
A reverseQuery() 0 4 1
A getName() 0 4 1
A geocode() 13 13 1
A limit() 0 6 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

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 package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @license    MIT License
9
 */
10
11
use Geocoder\Dumper\GeoJson;
12
use Geocoder\Dumper\Gpx;
13
use Geocoder\Dumper\Kml;
14
use Geocoder\Dumper\Wkb;
15
use Geocoder\Dumper\Wkt;
16
use Geocoder\Geocoder;
17
use Geocoder\Laravel\Exceptions\InvalidDumperException;
18
use Geocoder\Laravel\ProviderAndDumperAggregator;
19
use Geocoder\ProviderAggregator;
20
use Geocoder\Model\AddressCollection;
21
use Illuminate\Support\Collection;
22
23
/**
24
 * @author Mike Bronner <[email protected]>
25
 */
26
class ProviderAndDumperAggregator
27
{
28
    protected $results;
29
    protected $aggregator;
30
31
    public function __construct(int $limit = Geocoder::DEFAULT_RESULT_LIMIT)
32
    {
33
        $this->aggregator = new ProviderAggregator($limit);
34
    }
35
36
    public function all() : array
37
    {
38
        return $this->results->all();
39
    }
40
41
    public function get() : AddressCollection
42
    {
43
        return $this->results;
44
    }
45
46
    public function dump($dumper) : Collection
47
    {
48
        $dumperClasses = collect([
49
            'geojson' => GeoJson::class,
50
            'gpx' => Gpx::class,
51
            'kml' => Kml::class,
52
            'wkb' => Wkb::class,
53
            'wkt' => Wkt::class,
54
        ]);
55
56
        if (!$dumperClasses->has($dumper)) {
57
            $errorMessage = implode('', [
58
                "The dumper specified ('{$dumper}') is invalid. Valid dumpers ",
59
                "are: geojson, gpx, kml, wkb, wkt.",
60
            ]);
61
            throw new InvalidDumperException($errorMessage);
62
        }
63
64
        $dumperClass = $dumperClasses->get($dumper);
65
        $dumper = new $dumperClass;
66
        $results = collect($this->results->all());
67
68
        return $results->map(function ($result) use ($dumper) {
69
            return $dumper->dump($result);
70
        });
71
    }
72
73
    public function geocodeQuery($query)
74
    {
75
        return $this->aggregator->geocodeQuery($query);
76
    }
77
78
    public function reverseQuery($query)
79
    {
80
        return $this->aggregator->reverseQuery($query);
81
    }
82
83
    public function getName()
84
    {
85
        return $this->aggregator->getName();
86
    }
87
88 View Code Duplication
    public function geocode(string $value) : self
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        $cacheId = str_slug($value);
91
        $this->results = cache()->remember(
92
            "geocoder-{$cacheId}",
93
            config('geocoder.cache-duraction', 0),
94
            function () use ($value) {
95
                return $this->aggregator->geocode($value);
96
            }
97
        );
98
99
        return $this;
100
    }
101
102 View Code Duplication
    public function reverse(float $latitude, float $longitude) : self
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $cacheId = str_slug("{$latitude}-{$longitude}");
105
        $this->results = cache()->remember(
106
            "geocoder-{$cacheId}",
107
            config('geocoder.cache-duraction', 0),
108
            function () use ($latitude, $longitude) {
109
                return $this->aggregator->reverse($latitude, $longitude);
110
            }
111
        );
112
113
        return $this;
114
    }
115
116
    public function limit($limit)
117
    {
118
        $this->aggregator->limit($limit);
119
120
        return $this;
121
    }
122
123
    public function getLimit()
124
    {
125
        return $this->aggregator->getLimit();
126
    }
127
128
    public function registerProvider($provider)
129
    {
130
        $this->aggregator->registerProvider($provider);
131
132
        return $this;
133
    }
134
135
    public function registerProviders($providers = [])
136
    {
137
        $this->aggregator->registerProviders($providers);
138
139
        return $this;
140
    }
141
142
    public function using($name)
143
    {
144
        $this->aggregator->using($name);
145
146
        return $this;
147
    }
148
149
    public function getProviders()
150
    {
151
        return $this->aggregator->getProviders();
152
    }
153
154
    protected function getProvider()
155
    {
156
        return $this->aggregator->getProvider();
1 ignored issue
show
Bug introduced by
The method getProvider() cannot be called from this context as it is declared protected in class Geocoder\ProviderAggregator.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
157
    }
158
}
159