GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ProviderAggregator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 169
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A geocodeQuery() 0 8 2
A reverseQuery() 0 8 2
A getName() 0 4 1
A geocode() 0 5 1
A reverse() 0 5 1
A registerProvider() 0 6 1
A registerProviders() 0 8 2
A using() 0 10 2
A getProviders() 0 4 1
A getProvider() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder;
14
15
use Geocoder\Exception\ProviderNotRegistered;
16
use Geocoder\Model\Coordinates;
17
use Geocoder\Query\GeocodeQuery;
18
use Geocoder\Query\ReverseQuery;
19
use Geocoder\Provider\Provider;
20
21
/**
22
 * @author William Durand <[email protected]>
23
 */
24
class ProviderAggregator implements Geocoder
25
{
26
    /**
27
     * @var Provider[]
28
     */
29
    private $providers = [];
30
31
    /**
32
     * @var Provider
33
     */
34
    private $provider;
35
36
    /**
37
     * @var int
38
     */
39
    private $limit;
40
41
    /**
42
     * A callable that decided what provider to use.
43
     *
44
     * @var callable
45
     */
46
    private $decider;
47
48
    /**
49
     * @param callable|null $decider
50
     * @param int           $limit
51
     */
52
    public function __construct(callable $decider = null, int $limit = Geocoder::DEFAULT_RESULT_LIMIT)
53
    {
54
        $this->limit = $limit;
55
        $this->decider = $decider ?? __CLASS__.'::getProvider';
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function geocodeQuery(GeocodeQuery $query): Collection
62
    {
63
        if (null === $query->getLimit()) {
64
            $query = $query->withLimit($this->limit);
65
        }
66
67
        return call_user_func($this->decider, $query, $this->providers, $this->provider)->geocodeQuery($query);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function reverseQuery(ReverseQuery $query): Collection
74
    {
75
        if (null === $query->getLimit()) {
76
            $query = $query->withLimit($this->limit);
77
        }
78
79
        return call_user_func($this->decider, $query, $this->providers, $this->provider)->reverseQuery($query);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getName(): string
86
    {
87
        return 'provider_aggregator';
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function geocode(string $value): Collection
94
    {
95
        return $this->geocodeQuery(GeocodeQuery::create($value)
96
            ->withLimit($this->limit));
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function reverse(float $latitude, float $longitude): Collection
103
    {
104
        return $this->reverseQuery(ReverseQuery::create(new Coordinates($latitude, $longitude))
105
            ->withLimit($this->limit));
106
    }
107
108
    /**
109
     * Registers a new provider to the aggregator.
110
     *
111
     * @param Provider $provider
112
     *
113
     * @return ProviderAggregator
114
     */
115
    public function registerProvider(Provider $provider): self
116
    {
117
        $this->providers[$provider->getName()] = $provider;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Registers a set of providers.
124
     *
125
     * @param Provider[] $providers
126
     *
127
     * @return ProviderAggregator
128
     */
129
    public function registerProviders(array $providers = []): self
130
    {
131
        foreach ($providers as $provider) {
132
            $this->registerProvider($provider);
133
        }
134
135
        return $this;
136
    }
137
138
    /**
139
     * Sets the default provider to use.
140
     *
141
     * @param string $name
142
     *
143
     * @return ProviderAggregator
144
     */
145
    public function using(string $name): self
146
    {
147
        if (!isset($this->providers[$name])) {
148
            throw ProviderNotRegistered::create($name ?? '', $this->providers);
149
        }
150
151
        $this->provider = $this->providers[$name];
152
153
        return $this;
154
    }
155
156
    /**
157
     * Returns all registered providers indexed by their name.
158
     *
159
     * @return Provider[]
160
     */
161
    public function getProviders(): array
162
    {
163
        return $this->providers;
164
    }
165
166
    /**
167
     * Get a provider to use for this query.
168
     *
169
     * @param GeocodeQuery|ReverseQuery $query
170
     * @param Provider[]                $providers
171
     * @param Provider                  $currentProvider
0 ignored issues
show
Documentation introduced by
Should the type for parameter $currentProvider not be null|Provider?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
172
     *
173
     * @return Provider
174
     *
175
     * @throws ProviderNotRegistered
176
     */
177
    private static function getProvider($query, array $providers, Provider $currentProvider = null): Provider
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
178
    {
179
        if (null !== $currentProvider) {
180
            return $currentProvider;
181
        }
182
183
        if (0 === count($providers)) {
184
            throw ProviderNotRegistered::noProviderRegistered();
185
        }
186
187
        // Take first
188
        $key = key($providers);
189
190
        return $providers[$key];
191
    }
192
}
193