Completed
Push — master ( dd63f0...0f600a )
by Tobias
03:05
created

PluginProvider::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Plugin;
14
15
use Geocoder\Collection;
16
use Geocoder\Exception\Exception;
17
use Geocoder\Exception\LogicException;
18
use Geocoder\Plugin\Promise\GeocoderFulfilledPromise;
19
use Geocoder\Plugin\Promise\GeocoderRejectedPromise;
20
use Geocoder\Provider\Provider;
21
use Geocoder\Query\GeocodeQuery;
22
use Geocoder\Query\Query;
23
use Geocoder\Query\ReverseQuery;
24
use Geocoder\Plugin\Exception\LoopException;
25
26
/**
27
 * @author Joel Wurtz <[email protected]>
28
 * @author Tobias Nyholm <[email protected]>
29
 */
30
class PluginProvider implements Provider
31
{
32
    /**
33
     * @var Provider
34
     */
35
    private $provider;
36
37
    /**
38
     * @var Plugin[]
39
     */
40
    private $plugins;
41
42
    /**
43
     * A list of options.
44
     *
45
     * @var array
46
     */
47
    private $options;
48
49
    /**
50
     * @param Provider $provider
51
     * @param Plugin[] $plugins
52
     * @param array    $options  {
53
     *
54
     *     @var int      $max_restarts
55
     * }
56
     */
57 6
    public function __construct(Provider $provider, array $plugins = [], array $options = [])
58
    {
59 6
        $this->provider = $provider;
60 6
        $this->plugins = $plugins;
61 6
        $this->options = $this->configure($options);
62 6
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 5 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...
68
    {
69 5
        $pluginChain = $this->createPluginChain($this->plugins, function (GeocodeQuery $query) {
70
            try {
71 4
                return new GeocoderFulfilledPromise($this->provider->geocodeQuery($query));
72 1
            } catch (Exception $exception) {
73 1
                return new GeocoderRejectedPromise($exception);
74
            }
75 5
        });
76
77 5
        return $pluginChain($query)->wait();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 2 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...
84
    {
85 2
        $pluginChain = $this->createPluginChain($this->plugins, function (ReverseQuery $query) {
86
            try {
87 2
                return new GeocoderFulfilledPromise($this->provider->reverseQuery($query));
88
            } catch (Exception $exception) {
89
                return new GeocoderRejectedPromise($exception);
90
            }
91 2
        });
92
93 2
        return $pluginChain($query)->wait();
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getName(): string
100
    {
101
        return $this->provider->getName();
102
    }
103
104
    /**
105
     * Configure the plugin provider.
106
     *
107
     * @param array $options
108
     *
109
     * @return array
110
     */
111 6
    private function configure(array $options = []): array
112
    {
113
        $defaults = [
114 6
            'max_restarts' => 10,
115
        ];
116
117 6
        $config = array_merge($defaults, $options);
118
119
        // Make sure no invalid values are provided
120 6
        if (count($config) !== count($defaults)) {
121
            throw new LogicException(sprintf('Valid options to the PluginProviders are: %s', implode(', ', array_values($defaults))));
122
        }
123
124 6
        return $config;
125
    }
126
127
    /**
128
     * Create the plugin chain.
129
     *
130
     * @param Plugin[] $pluginList     A list of plugins
131
     * @param callable $clientCallable Callable making the HTTP call
132
     *
133
     * @return callable
134
     */
135 6
    private function createPluginChain(array $pluginList, callable $clientCallable)
136
    {
137 6
        $firstCallable = $lastCallable = $clientCallable;
138
139 6
        while ($plugin = array_pop($pluginList)) {
140 5
            $lastCallable = function (Query $query) use ($plugin, $lastCallable, &$firstCallable) {
141 5
                return $plugin->handleQuery($query, $lastCallable, $firstCallable);
142 5
            };
143
144 5
            $firstCallable = $lastCallable;
145
        }
146
147 6
        $firstCalls = 0;
148 6
        $firstCallable = function (Query $query) use ($lastCallable, &$firstCalls) {
149 6
            if ($firstCalls > $this->options['max_restarts']) {
150 1
                throw LoopException::create('Too many restarts in plugin provider', $query);
151
            }
152
153 6
            ++$firstCalls;
154
155 6
            return $lastCallable($query);
156 6
        };
157
158 6
        return $firstCallable;
159
    }
160
}
161