Completed
Push — master ( d0ebd1...90b63b )
by Tobias
01:49
created

ProviderAggregator::limit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 9
    public function __construct(callable $decider = null, int $limit = Geocoder::DEFAULT_RESULT_LIMIT)
53
    {
54 9
        $this->limit = $limit;
55 9
        $this->decider = $decider ?? __CLASS__.'::getProvider';
56 9
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1 View Code Duplication
    public function geocodeQuery(GeocodeQuery $query): Collection
0 ignored issues
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...
62
    {
63 1
        if (null === $query->getLimit()) {
64
            $query = $query->withLimit($this->limit);
65
        }
66
67 1
        return call_user_func($this->decider, $query, $this->providers, $this->provider)->geocodeQuery($query);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1 View Code Duplication
    public function reverseQuery(ReverseQuery $query): Collection
0 ignored issues
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...
74
    {
75 1
        if (null === $query->getLimit()) {
76
            $query = $query->withLimit($this->limit);
77
        }
78
79 1
        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 1
    public function geocode(string $value): Collection
94
    {
95 1
        return $this->geocodeQuery(GeocodeQuery::create($value)
96 1
            ->withLimit($this->limit));
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 1
    public function reverse(float $latitude, float $longitude): Collection
103
    {
104 1
        return $this->reverseQuery(ReverseQuery::create(new Coordinates($latitude, $longitude))
105 1
            ->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 5
    public function registerProvider(Provider $provider): self
116
    {
117 5
        $this->providers[$provider->getName()] = $provider;
118
119 5
        return $this;
120
    }
121
122
    /**
123
     * Registers a set of providers.
124
     *
125
     * @param Provider[] $providers
126
     *
127
     * @return ProviderAggregator
128
     */
129 2
    public function registerProviders(array $providers = []): self
130
    {
131 2
        foreach ($providers as $provider) {
132 2
            $this->registerProvider($provider);
133
        }
134
135 2
        return $this;
136
    }
137
138
    /**
139
     * Sets the default provider to use.
140
     *
141
     * @param string $name
142
     *
143
     * @return ProviderAggregator
144
     */
145 3
    public function using(string $name): self
146
    {
147 3
        if (!isset($this->providers[$name])) {
148 2
            throw ProviderNotRegistered::create($name ?? '', $this->providers);
149
        }
150
151 1
        $this->provider = $this->providers[$name];
152
153 1
        return $this;
154
    }
155
156
    /**
157
     * Returns all registered providers indexed by their name.
158
     *
159
     * @return Provider[]
160
     */
161 1
    public function getProviders(): array
162
    {
163 1
        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
172
     *
173
     * @return Provider
174
     *
175
     * @throws ProviderNotRegistered
176
     */
177 4
    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 4
        if (null !== $currentProvider) {
180 2
            return $currentProvider;
181
        }
182
183 3
        if (0 === count($providers)) {
184 1
            throw ProviderNotRegistered::noProviderRegistered();
185
        }
186
187
        // Take first
188 2
        $key = key($providers);
189
190 2
        return $providers[$key];
191
    }
192
}
193