LaraCrafts /
laravel-url-shortener
| 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 | /** |
||||
| 21 | * @method string shorten(\Psr\Http\Message\UriInterface|string $url, array $options = []) |
||||
| 22 | */ |
||||
| 23 | class UrlShortenerManager implements FactoryContract |
||||
| 24 | { |
||||
| 25 | protected $app; |
||||
| 26 | protected $customCreators; |
||||
| 27 | protected $shorteners; |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * Create a new URL shortener manager instance. |
||||
| 31 | * |
||||
| 32 | * @param \Illuminate\Contracts\Foundation\Application $app |
||||
| 33 | * @return void |
||||
| 34 | */ |
||||
| 35 | public function __construct(Application $app) |
||||
| 36 | { |
||||
| 37 | $this->app = $app; |
||||
| 38 | $this->customCreators = []; |
||||
| 39 | $this->shorteners = []; |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * Dynamically call the default driver instance. |
||||
| 44 | * |
||||
| 45 | * @param string $method |
||||
| 46 | * @param array $parameters |
||||
| 47 | * @return mixed |
||||
| 48 | */ |
||||
| 49 | public function __call($method, $parameters) |
||||
| 50 | { |
||||
| 51 | return $this->driver()->$method(...$parameters); |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * Call a custom driver creator. |
||||
| 56 | * |
||||
| 57 | * @param array $config |
||||
| 58 | * @return mixed |
||||
| 59 | */ |
||||
| 60 | protected function callCustomCreator(array $config) |
||||
| 61 | { |
||||
| 62 | return $this->customCreators[$config['driver']]($this->app, $config); |
||||
| 63 | } |
||||
| 64 | |||||
| 65 | /** |
||||
| 66 | * Create an instance of the Bit.ly driver. |
||||
| 67 | * |
||||
| 68 | * @param array $config |
||||
| 69 | * @return \LaraCrafts\UrlShortener\Http\BitLyShortener |
||||
| 70 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||||
| 71 | */ |
||||
| 72 | protected function createBitLyDriver(array $config) |
||||
| 73 | { |
||||
| 74 | return new BitLyShortener( |
||||
| 75 | $this->app->make(ClientInterface::class), |
||||
| 76 | Arr::get($config, 'token'), |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 77 | Arr::get($config, 'domain', 'bit.ly') |
||||
| 78 | ); |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | /** |
||||
| 82 | * Create an instance of the Firebase driver. |
||||
| 83 | * |
||||
| 84 | * @param array $config |
||||
| 85 | * @return \LaraCrafts\UrlShortener\Http\FirebaseShortener |
||||
| 86 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||||
| 87 | */ |
||||
| 88 | protected function createFirebaseDriver(array $config) |
||||
| 89 | { |
||||
| 90 | return new FirebaseShortener( |
||||
| 91 | $this->app->make(ClientInterface::class), |
||||
| 92 | Arr::get($config, 'token'), |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 93 | Arr::get($config, 'prefix'), |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 94 | Arr::get($config, 'suffix') |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 95 | ); |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | /** |
||||
| 99 | * Create an instance of the Is.gd driver. |
||||
| 100 | * |
||||
| 101 | * @param array $config |
||||
| 102 | * @return \LaraCrafts\UrlShortener\Http\IsGdShortener |
||||
| 103 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||||
| 104 | */ |
||||
| 105 | protected function createIsGdDriver(array $config) |
||||
| 106 | { |
||||
| 107 | return new IsGdShortener( |
||||
| 108 | $this->app->make(ClientInterface::class), |
||||
| 109 | Arr::get($config, 'base_uri'), |
||||
| 110 | Arr::get($config, 'statistics') |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 111 | ); |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | /** |
||||
| 115 | * Create an instance of the Ouo.io driver. |
||||
| 116 | * |
||||
| 117 | * @param array $config |
||||
| 118 | * @return \LaraCrafts\UrlShortener\Http\OuoIoShortener |
||||
| 119 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||||
| 120 | */ |
||||
| 121 | protected function createOuoIoDriver(array $config) |
||||
| 122 | { |
||||
| 123 | return new OuoIoShortener( |
||||
| 124 | $this->app->make(ClientInterface::class), |
||||
| 125 | Arr::get($config, 'token') |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 126 | ); |
||||
| 127 | } |
||||
| 128 | |||||
| 129 | /** |
||||
| 130 | * Create an instance of the Polr driver. |
||||
| 131 | * |
||||
| 132 | * @param array $config |
||||
| 133 | * @return \LaraCrafts\UrlShortener\Http\PolrShortener |
||||
| 134 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||||
| 135 | */ |
||||
| 136 | protected function createPolrDriver(array $config) |
||||
| 137 | { |
||||
| 138 | return new PolrShortener( |
||||
| 139 | $this->app->make(ClientInterface::class), |
||||
| 140 | Arr::get($config, 'token'), |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 141 | Arr::get($config, 'prefix') |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 142 | ); |
||||
| 143 | } |
||||
| 144 | |||||
| 145 | /** |
||||
| 146 | * Create an instance of the Shorte.st driver. |
||||
| 147 | * |
||||
| 148 | * @param array $config |
||||
| 149 | * @return \LaraCrafts\UrlShortener\Http\ShorteStShortener |
||||
| 150 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||||
| 151 | */ |
||||
| 152 | protected function createShorteStDriver(array $config) |
||||
| 153 | { |
||||
| 154 | return new ShorteStShortener( |
||||
| 155 | $this->app->make(ClientInterface::class), |
||||
| 156 | Arr::get($config, 'token') |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 157 | ); |
||||
| 158 | } |
||||
| 159 | |||||
| 160 | /** |
||||
| 161 | * Create an instance of the TinyURL driver. |
||||
| 162 | * |
||||
| 163 | * @return \LaraCrafts\UrlShortener\Http\TinyUrlShortener |
||||
| 164 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||||
| 165 | */ |
||||
| 166 | protected function createTinyUrlDriver() |
||||
| 167 | { |
||||
| 168 | return new TinyUrlShortener($this->app->make(ClientInterface::class)); |
||||
| 169 | } |
||||
| 170 | |||||
| 171 | /** |
||||
| 172 | * Get a URL shortener driver instance. |
||||
| 173 | * |
||||
| 174 | * @param string|null $name |
||||
| 175 | * @return \LaraCrafts\UrlShortener\Contracts\Shortener |
||||
| 176 | */ |
||||
| 177 | public function driver(string $name = null) |
||||
| 178 | { |
||||
| 179 | return $this->shortener($name); |
||||
| 180 | } |
||||
| 181 | |||||
| 182 | /** |
||||
| 183 | * Register a custom driver creator closure. |
||||
| 184 | * |
||||
| 185 | * @param string $name |
||||
| 186 | * @param \Closure $callback |
||||
| 187 | * @return $this |
||||
| 188 | */ |
||||
| 189 | public function extend(string $name, Closure $callback) |
||||
| 190 | { |
||||
| 191 | $this->customCreators[$name] = $callback->bindTo($this, $this); |
||||
| 192 | |||||
| 193 | return $this; |
||||
| 194 | } |
||||
| 195 | |||||
| 196 | /** |
||||
| 197 | * Get the default URL shortener driver name. |
||||
| 198 | * |
||||
| 199 | * @return string |
||||
| 200 | */ |
||||
| 201 | public function getDefaultDriver() |
||||
| 202 | { |
||||
| 203 | return $this->app['config']['url-shortener.default']; |
||||
| 204 | } |
||||
| 205 | |||||
| 206 | /** |
||||
| 207 | * Get the URL shortener configuration. |
||||
| 208 | * |
||||
| 209 | * @param string $name |
||||
| 210 | * @return array|null |
||||
| 211 | */ |
||||
| 212 | protected function getShortenerConfig(string $name) |
||||
| 213 | { |
||||
| 214 | return $this->app['config']["url-shortener.shorteners.$name"]; |
||||
| 215 | } |
||||
| 216 | |||||
| 217 | /** |
||||
| 218 | * Resolve the given URL shortener. |
||||
| 219 | * |
||||
| 220 | * @param string $name |
||||
| 221 | * @return \LaraCrafts\UrlShortener\Contracts\Shortener |
||||
| 222 | */ |
||||
| 223 | protected function resolve(string $name) |
||||
| 224 | { |
||||
| 225 | $config = $this->getShortenerConfig($name); |
||||
| 226 | |||||
| 227 | if (is_null($config) || !array_key_exists('driver', $config)) { |
||||
| 228 | throw new InvalidArgumentException("URL shortener [{$name}] is not defined"); |
||||
| 229 | } |
||||
| 230 | |||||
| 231 | if (array_key_exists($config['driver'], $this->customCreators)) { |
||||
| 232 | return $this->callCustomCreator($config); |
||||
| 233 | } |
||||
| 234 | |||||
| 235 | $driverMethod = 'create' . Str::studly($config['driver']) . 'Driver'; |
||||
| 236 | |||||
| 237 | if (method_exists($this, $driverMethod)) { |
||||
| 238 | return $this->$driverMethod($config); |
||||
| 239 | } |
||||
| 240 | throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported"); |
||||
| 241 | } |
||||
| 242 | |||||
| 243 | /** |
||||
| 244 | * Set the default URL shortener driver name. |
||||
| 245 | * |
||||
| 246 | * @param string $name |
||||
| 247 | * @return $this |
||||
| 248 | */ |
||||
| 249 | public function setDefaultDriver(string $name) |
||||
| 250 | { |
||||
| 251 | $this->app['config']['url-shortener.default'] = $name; |
||||
| 252 | |||||
| 253 | return $this; |
||||
| 254 | } |
||||
| 255 | |||||
| 256 | /** |
||||
| 257 | * {@inheritDoc} |
||||
| 258 | */ |
||||
| 259 | public function shortener(string $name = null) |
||||
| 260 | { |
||||
| 261 | $name = $name ?: $this->getDefaultDriver(); |
||||
| 262 | |||||
| 263 | if (array_key_exists($name, $this->shorteners)) { |
||||
| 264 | return $this->shorteners[$name]; |
||||
| 265 | } |
||||
| 266 | |||||
| 267 | return $this->shorteners[$name] = $this->resolve($name); |
||||
| 268 | } |
||||
| 269 | } |
||||
| 270 |