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 (#21)
by Choraimy
12:44 queued 05:14
created

UrlShortenerManager::setDefaultDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
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\ShorteStShortener;
17
use LaraCrafts\UrlShortener\Http\TinyUrlShortener;
18
19
class UrlShortenerManager implements FactoryContract
20
{
21
    protected $app;
22
    protected $customCreators;
23
    protected $shorteners;
24
25
    /**
26
     * Create a new URL shortener manager instance.
27
     *
28 24
     * @param \Illuminate\Contracts\Foundation\Application $app
29
     * @return void
30 24
     */
31
    public function __construct(Application $app)
32 24
    {
33 24
        $this->app = $app;
34 24
        $this->customCreators = [];
35 24
        $this->shorteners = [];
36
    }
37
38
    /**
39
     * Call a custom driver creator.
40
     *
41
     * @param array $config
42 168
     * @return mixed
43
     */
44
    protected function callCustomCreator(array $config)
45 168
    {
46 168
        return $this->customCreators[$config['driver']]($this->app, $config);
47
    }
48
49
    /**
50
     * Create an instance of the Bit.ly driver.
51
     *
52
     * @param array $config
53
     * @return \LaraCrafts\UrlShortener\Http\BitLyShortener
54
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
55
     */
56
    protected function createBitLyDriver(array $config)
57
    {
58 24
        return new BitLyShortener(
59
            $this->app->make(ClientInterface::class),
60 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

60
            /** @scrutinizer ignore-type */ Arr::get($config, 'token'),
Loading history...
61
            Arr::get($config, 'domain', 'bit.ly')
62 24
        );
63 24
    }
64 24
65 24
    /**
66 24
     * Create an instance of the Firebase driver.
67
     *
68
     * @param array $config
69
     * @return \LaraCrafts\UrlShortener\Http\FirebaseShortener
70
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
71
     */
72
    protected function createFirebaseDriver(array $config)
73
    {
74
        return new FirebaseShortener(
75
            $this->app->make(ClientInterface::class),
76 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

76
            /** @scrutinizer ignore-type */ Arr::get($config, 'token'),
Loading history...
77
            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

77
            /** @scrutinizer ignore-type */ Arr::get($config, 'prefix'),
Loading history...
78 24
            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

78
            /** @scrutinizer ignore-type */ Arr::get($config, 'suffix')
Loading history...
79
        );
80 24
    }
81 24
82 24
    /**
83 24
     * Create an instance of the Is.gd driver.
84
     *
85
     * @param array $config
86
     * @return \LaraCrafts\UrlShortener\Http\IsGdShortener
87
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
88
     */
89
    protected function createIsGdDriver(array $config)
90
    {
91
        return new IsGdShortener(
92
            $this->app->make(ClientInterface::class),
93 24
            Arr::get($config, 'base_uri'),
94
            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

94
            /** @scrutinizer ignore-type */ Arr::get($config, 'statistics')
Loading history...
95 24
        );
96
    }
97 24
98 24
    /**
99 24
     * Create an instance of the Ouo.io driver.
100
     *
101
     * @param array $config
102
     * @return \LaraCrafts\UrlShortener\Http\OuoIoShortener
103
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
104
     */
105
    protected function createOuoIoDriver(array $config)
106
    {
107
        return new OuoIoShortener(
108
            $this->app->make(ClientInterface::class),
109 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

109
            /** @scrutinizer ignore-type */ Arr::get($config, 'token')
Loading history...
110
        );
111 24
    }
112
113 24
    /**
114 24
     * Create an instance of the Shorte.st driver.
115 24
     *
116
     * @param array $config
117
     * @return \LaraCrafts\UrlShortener\Http\ShorteStShortener
118
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
119
     */
120
    protected function createShorteStDriver(array $config)
121
    {
122
        return new ShorteStShortener(
123
            $this->app->make(ClientInterface::class),
124
            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

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