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.
Completed
Push — master ( b0e03a...4af0c2 )
by Tobias
02:50
created

PluginProvider::geocodeQuery()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 1
nop 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\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
    public function __construct(Provider $provider, array $plugins = [], array $options = [])
58
    {
59
        $this->provider = $provider;
60
        $this->plugins = $plugins;
61
        $this->options = $this->configure($options);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function geocodeQuery(GeocodeQuery $query): Collection
68
    {
69
        $pluginChain = $this->createPluginChain($this->plugins, function (GeocodeQuery $query) {
70
            try {
71
                return new GeocoderFulfilledPromise($this->provider->geocodeQuery($query));
72
            } catch (Exception $exception) {
73
                return new GeocoderRejectedPromise($exception);
74
            }
75
        });
76
77
        return $pluginChain($query)->wait();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function reverseQuery(ReverseQuery $query): Collection
84
    {
85
        $pluginChain = $this->createPluginChain($this->plugins, function (ReverseQuery $query) {
86
            try {
87
                return new GeocoderFulfilledPromise($this->provider->reverseQuery($query));
88
            } catch (Exception $exception) {
89
                return new GeocoderRejectedPromise($exception);
90
            }
91
        });
92
93
        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
    private function configure(array $options = []): array
112
    {
113
        $defaults = [
114
            'max_restarts' => 10,
115
        ];
116
117
        $config = array_merge($defaults, $options);
118
119
        // Make sure no invalid values are provided
120
        if (count($config) !== count($defaults)) {
121
            throw new LogicException(sprintf('Valid options to the PluginProviders are: %s', implode(', ', array_values($defaults))));
122
        }
123
124
        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
    private function createPluginChain(array $pluginList, callable $clientCallable)
136
    {
137
        $firstCallable = $lastCallable = $clientCallable;
138
139
        while ($plugin = array_pop($pluginList)) {
140
            $lastCallable = function (Query $query) use ($plugin, $lastCallable, &$firstCallable) {
141
                return $plugin->handleQuery($query, $lastCallable, $firstCallable);
142
            };
143
144
            $firstCallable = $lastCallable;
145
        }
146
147
        $firstCalls = 0;
148
        $firstCallable = function (Query $query) use ($lastCallable, &$firstCalls) {
149
            if ($firstCalls > $this->options['max_restarts']) {
150
                throw LoopException::create('Too many restarts in plugin provider', $query);
151
            }
152
153
            ++$firstCalls;
154
155
            return $lastCallable($query);
156
        };
157
158
        return $firstCallable;
159
    }
160
}
161