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
Pull Request — master (#35)
by Choraimy
08:47
created

UrlShortenerManager::createOuoIoDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
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
     */
32
    public function __construct(Application $app)
33
    {
34
        $this->app = $app;
35
        $this->customCreators = [];
36
        $this->shorteners = [];
37
    }
38
39
    /**
40
     * Call a custom driver creator.
41
     *
42
     * @param array $config
43
     * @return mixed
44
     */
45
    protected function callCustomCreator(array $config)
46
    {
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
     */
57
    protected function createBitLyDriver(array $config)
58
    {
59
        return new BitLyShortener(
60
            $this->app->make(ClientInterface::class),
61
            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
     */
73
    protected function createFirebaseDriver(array $config)
74
    {
75
        return new FirebaseShortener(
76
            $this->app->make(ClientInterface::class),
77
            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
            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
     */
90
    protected function createIsGdDriver(array $config)
91
    {
92
        return new IsGdShortener(
93
            $this->app->make(ClientInterface::class),
94
            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
     */
106
    protected function createOuoIoDriver(array $config)
107
    {
108
        return new OuoIoShortener(
109
            $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
     */
121
    protected function createPolrDriver(array $config)
122
    {
123
        return new PolrShortener(
124
            $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
     * @return \LaraCrafts\UrlShortener\Http\ShorteStShortener
135
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
136
     */
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
    /**
146
     * Create an instance of the TinyURL driver.
147
     *
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
     * Get a URL shortener driver instance.
158
     *
159
     * @param string|null $name
160
     * @return \LaraCrafts\UrlShortener\Contracts\Shortener
161
     */
162
    public function driver(string $name = null)
163
    {
164
        return $this->shortener($name);
165
    }
166
167
    /**
168
     * Register a custom driver creator closure.
169
     *
170
     * @param string $name
171
     * @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
181
    /**
182
     * 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
    /**
192
     * Get the URL shortener configuration.
193
     *
194
     * @param string $name
195
     * @return array|null
196
     */
197
    protected function getShortenerConfig(string $name)
198
    {
199
        return $this->app['config']["url-shortener.shorteners.$name"];
200
    }
201
202
    /**
203
     * Resolve the given URL shortener.
204
     *
205
     * @param string $name
206
     * @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
            return $this->callCustomCreator($config);
218
        }
219
220
        $driverMethod = 'create' . Str::studly($config['driver']) . 'Driver';
221
222
        if (method_exists($this, $driverMethod)) {
223
            return $this->$driverMethod($config);
224
        }
225
        throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported");
226
    }
227
228
    /**
229
     * Set the default URL shortener driver name.
230
     *
231
     * @param string $name
232
     * @return $this
233
     */
234
    public function setDefaultDriver(string $name)
235
    {
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