UrlsShortenerServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace DmitryBubyakin\Shortener;
4
5
use Hashids\Hashids;
6
use Illuminate\Support\ServiceProvider;
7
8
class UrlsShortenerServiceProvider extends ServiceProvider
9
{
10
    public function boot(): void
11
    {
12
        $this->publishes([
13
            __DIR__.'/../config/config.php' => config_path('laravel-urls-shortener.php'),
14
        ]);
15
16
        if (! class_exists('CreateShortUrlsTable')) {
17
            $this->publishes([
18
                __DIR__.'/../database/migrations/create_short_urls_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_short_urls_table.php'),
19
            ]);
20
        }
21
    }
22
23
    public function register(): void
24
    {
25
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-urls-shortener');
26
27
        $this->app->bind(Shortener::class, function () {
28
            $hash = $this->createHashids();
29
            $route = config('laravel-urls-shortener.route');
30
31
            return new Shortener($hash, $route);
32
        });
33
34
        $this->app->alias(Shortener::class, 'shortener');
35
    }
36
37
    public function createHashids(): Hashids
38
    {
39
        return new Hashids(
40
            config('laravel-urls-shortener.hashids.salt'),
41
            config('laravel-urls-shortener.hashids.min_hash_length'),
42
            config('laravel-urls-shortener.hashids.alphabet')
43
        );
44
    }
45
}
46