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.
Passed
Pull Request — master (#22)
by
unknown
17:36
created

UrlShortenerManager::createPolrDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LaraCrafts\UrlShortener;
4
5
use Closure;
6
use GuzzleHttp\ClientInterface;
7
use Illuminate\Contracts\Foundation\Application;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Str;
10
use InvalidArgumentException;
11
use LaraCrafts\UrlShortener\Contracts\Factory as FactoryContract;
12
use LaraCrafts\UrlShortener\Http\BitLyShortener;
13
use LaraCrafts\UrlShortener\Http\FirebaseShortener;
14
use LaraCrafts\UrlShortener\Http\IsGdShortener;
15
use LaraCrafts\UrlShortener\Http\OuoIoShortener;
16
use LaraCrafts\UrlShortener\Http\PolrShortener;
17
use LaraCrafts\UrlShortener\Http\ShorteStShortener;
18
use LaraCrafts\UrlShortener\Http\TinyUrlShortener;
19
20
class UrlShortenerManager implements FactoryContract
21
{
22
    protected $app;
23
    protected $customCreators;
24
    protected $shorteners;
25
26
    /**
27
     * Create a new URL shortener manager instance.
28
     *
29
     * @param \Illuminate\Contracts\Foundation\Application $app
30
     * @return void
31 264
     */
32
    public function __construct(Application $app)
33 264
    {
34 264
        $this->app = $app;
35 264
        $this->customCreators = [];
36 264
        $this->shorteners = [];
37
    }
38
39
    /**
40
     * Call a custom driver creator.
41
     *
42
     * @param array $config
43
     * @return mixed
44 24
     */
45
    protected function callCustomCreator(array $config)
46 24
    {
47
        return $this->customCreators[$config['driver']]($this->app, $config);
48
    }
49
50
    /**
51
     * Create an instance of the Bit.ly driver.
52
     *
53
     * @param array $config
54
     * @return \LaraCrafts\UrlShortener\Http\BitLyShortener
55
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
56 24
     */
57
    protected function createBitLyDriver(array $config)
58 24
    {
59 24
        return new BitLyShortener(
60 24
            $this->app->make(ClientInterface::class),
61 24
            Arr::get($config, 'token'),
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::get($config, 'token') can also be of type null; however, parameter $token of LaraCrafts\UrlShortener\...hortener::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            /** @scrutinizer ignore-type */ Arr::get($config, 'token'),
Loading history...
62
            Arr::get($config, 'domain', 'bit.ly')
63
        );
64
    }
65
66
    /**
67
     * Create an instance of the Firebase driver.
68
     *
69
     * @param array $config
70
     * @return \LaraCrafts\UrlShortener\Http\FirebaseShortener
71
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
72 24
     */
73
    protected function createFirebaseDriver(array $config)
74 24
    {
75 24
        return new FirebaseShortener(
76 24
            $this->app->make(ClientInterface::class),
77 24
            Arr::get($config, 'token'),
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::get($config, 'token') can also be of type null; however, parameter $token of LaraCrafts\UrlShortener\...hortener::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            /** @scrutinizer ignore-type */ Arr::get($config, 'token'),
Loading history...
78 24
            Arr::get($config, 'prefix'),
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::get($config, 'prefix') can also be of type null; however, parameter $domain of LaraCrafts\UrlShortener\...hortener::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            /** @scrutinizer ignore-type */ Arr::get($config, 'prefix'),
Loading history...
79
            Arr::get($config, 'suffix')
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::get($config, 'suffix') can also be of type null; however, parameter $suffix of LaraCrafts\UrlShortener\...hortener::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            /** @scrutinizer ignore-type */ Arr::get($config, 'suffix')
Loading history...
80
        );
81
    }
82
83
    /**
84
     * Create an instance of the Is.gd driver.
85
     *
86
     * @param array $config
87
     * @return \LaraCrafts\UrlShortener\Http\IsGdShortener
88
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
89 48
     */
90
    protected function createIsGdDriver(array $config)
91 48
    {
92 48
        return new IsGdShortener(
93 48
            $this->app->make(ClientInterface::class),
94 48
            Arr::get($config, 'base_uri'),
95
            Arr::get($config, 'statistics')
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::...($config, 'statistics') can also be of type null; however, parameter $statistics of LaraCrafts\UrlShortener\...hortener::__construct() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
            /** @scrutinizer ignore-type */ Arr::get($config, 'statistics')
Loading history...
96
        );
97
    }
98
99
    /**
100
     * Create an instance of the Ouo.io driver.
101
     *
102
     * @param array $config
103
     * @return \LaraCrafts\UrlShortener\Http\OuoIoShortener
104
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
105 24
     */
106
    protected function createOuoIoDriver(array $config)
107 24
    {
108 24
        return new OuoIoShortener(
109 24
            $this->app->make(ClientInterface::class),
110
            Arr::get($config, 'token')
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::get($config, 'token') can also be of type null; however, parameter $token of LaraCrafts\UrlShortener\...hortener::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            /** @scrutinizer ignore-type */ Arr::get($config, 'token')
Loading history...
111
        );
112
    }
113
114
    /**
115
     * Create an instance of the Polr driver.
116
     *
117
     * @param array $config
118
     * @return \LaraCrafts\UrlShortener\Http\PolrShortener
119
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
120 24
     */
121
    protected function createPolrDriver(array $config)
122 24
    {
123 24
        return new PolrShortener(
124 24
            $this->app->make(ClientInterface::class),
125
            Arr::get($config, 'token'),
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::get($config, 'token') can also be of type null; however, parameter $token of LaraCrafts\UrlShortener\...hortener::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
            /** @scrutinizer ignore-type */ Arr::get($config, 'token'),
Loading history...
126
            Arr::get($config, 'prefix')
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::get($config, 'prefix') can also be of type null; however, parameter $domain of LaraCrafts\UrlShortener\...hortener::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
            /** @scrutinizer ignore-type */ Arr::get($config, 'prefix')
Loading history...
127
        );
128
    }
129
130
    /**
131
     * Create an instance of the Shorte.st driver.
132
     *
133
     * @param array $config
134 48
     * @return \LaraCrafts\UrlShortener\Http\ShorteStShortener
135
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
136 48
     */
137
    protected function createShorteStDriver(array $config)
138
    {
139
        return new ShorteStShortener(
140
            $this->app->make(ClientInterface::class),
141
            Arr::get($config, 'token')
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::get($config, 'token') can also be of type null; however, parameter $token of LaraCrafts\UrlShortener\...hortener::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
            /** @scrutinizer ignore-type */ Arr::get($config, 'token')
Loading history...
142
        );
143
    }
144
145 24
    /**
146
     * Create an instance of the TinyURL driver.
147 24
     *
148
     * @return \LaraCrafts\UrlShortener\Http\TinyUrlShortener
149
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
150
     */
151
    protected function createTinyUrlDriver()
152
    {
153
        return new TinyUrlShortener($this->app->make(ClientInterface::class));
154
    }
155
156
    /**
157 24
     * Get a URL shortener driver instance.
158
     *
159 24
     * @param string|null $name
160
     * @return \LaraCrafts\UrlShortener\Contracts\Shortener
161 24
     */
162
    public function driver(string $name = null)
163
    {
164
        return $this->shortener($name);
165
    }
166
167
    /**
168
     * Register a custom driver creator closure.
169 48
     *
170
     * @param string $name
171 48
     * @param \Closure $callback
172
     * @return $this
173
     */
174
    public function extend(string $name, Closure $callback)
175
    {
176
        $this->customCreators[$name] = $callback->bindTo($this, $this);
177
178
        return $this;
179
    }
180 216
181
    /**
182 216
     * Get the default URL shortener driver name.
183
     *
184
     * @return string
185
     */
186
    public function getDefaultDriver()
187
    {
188
        return $this->app['config']['url-shortener.default'];
189
    }
190
191 216
    /**
192
     * Get the URL shortener configuration.
193 216
     *
194
     * @param string $name
195 216
     * @return array|null
196
     */
197
    protected function getShortenerConfig(string $name)
198
    {
199 216
        return $this->app['config']["url-shortener.shorteners.$name"];
200 24
    }
201
202
    /**
203 192
     * Resolve the given URL shortener.
204
     *
205 192
     * @param string $name
206 192
     * @return \LaraCrafts\UrlShortener\Contracts\Shortener
207
     */
208
    protected function resolve(string $name)
209
    {
210
        $config = $this->getShortenerConfig($name);
211
212
        if (is_null($config) || !array_key_exists('driver', $config)) {
213
            throw new InvalidArgumentException("URL shortener [{$name}] is not defined");
214
        }
215
216
        if (array_key_exists($config['driver'], $this->customCreators)) {
217 24
            return $this->callCustomCreator($config);
218
        }
219 24
220
        $driverMethod = 'create' . Str::studly($config['driver']) . 'Driver';
221 24
222
        if (method_exists($this, $driverMethod)) {
223
            return $this->$driverMethod($config);
224
        }
225
        throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported");
226
    }
227 216
228
    /**
229 216
     * Set the default URL shortener driver name.
230
     *
231 216
     * @param string $name
232
     * @return $this
233
     */
234
    public function setDefaultDriver(string $name)
235 216
    {
236
        $this->app['config']['url-shortener.default'] = $name;
237
238
        return $this;
239
    }
240
241
    /**
242
     * {@inheritDoc}
243
     */
244
    public function shortener(string $name = null)
245
    {
246
        $name = $name ?: $this->getDefaultDriver();
247
248
        if (array_key_exists($name, $this->shorteners)) {
249
            return $this->shorteners[$name];
250
        }
251
252
        return $this->shorteners[$name] = $this->resolve($name);
253
    }
254
}
255