Completed
Push — master ( 546fe9...d8d6e3 )
by Elf
12s
created

HashidServiceProvider::getSingletonBindings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 9.6666
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\ServiceProvider;
7
8
class HashidServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Register the service provider.
12
     *
13
     * @return void
14
     */
15 6
    public function register()
16
    {
17 6
        $this->setupAssets();
18
19 6
        $this->registerServices();
20 6
        $this->registerCommands();
21 6
    }
22
23
    /**
24
     * Setup package assets.
25
     *
26
     * @return void
27
     */
28 6
    protected function setupAssets()
29
    {
30 6
        if (is_a($this->app, 'Laravel\Lumen\Application')) {
31
            $this->app->configure('hashid');
32
        }
33
34 6
        $this->mergeConfigFrom($config = __DIR__.'/../config/hashid.php', 'hashid');
35
36 6
        if ($this->app->runningInConsole()) {
37 6
            $this->publishes([$config => base_path('config/hashid.php')], 'hashid');
38
        }
39 6
    }
40
41
    /**
42
     * Register service bindings.
43
     *
44
     * @return void
45
     */
46
    protected function registerServices()
47
    {
48 6
        $this->app->singleton('hashid', function ($app) {
49 4
            return new HashidManager($app);
50 6
        });
51 6
        $this->app->alias('hashid', HashidManager::class);
52
53
        foreach ([
54 6
            Base64Driver::class,
55
            Base64IntegerDriver::class,
56
            HexDriver::class,
57
            HexIntegerDriver::class,
58
        ] as $class) {
59 6
            $key = $this->getBindingKeyForDriver($class);
60
61 6
            $this->app->singleton($key, $class);
62 6
            $this->app->alias($key, $class);
63
        }
64
65
        foreach ([
66 6
            Base62Driver::class,
67
            Base62IntegerDriver::class,
68
            HashidsDriver::class,
69
            HashidsHexDriver::class,
70
            HashidsIntegerDriver::class,
71
            HashidsStringDriver::class,
72
            OptimusDriver::class,
73
        ] as $class) {
74 6
            $this->app->bind($this->getBindingKeyForDriver($class), $class);
75
        }
76 6
    }
77
78
    /**
79
     * Get the binding key for the driver class.
80
     *
81
     * @param  string  $class
82
     * @return string
83
     */
84 6
    protected function getBindingKeyForDriver($class)
85
    {
86 6
        return 'hashid.driver.'.Str::snake(
87 6
            preg_replace('#Driver$#', '', class_basename($class))
88
        );
89
    }
90
91
    /**
92
     * Register console commands.
93
     *
94
     * @return void
95
     */
96 6
    protected function registerCommands()
97
    {
98 6
        if ($this->app->runningInConsole()) {
99 6
            $this->commands([
100 6
                Console\AlphabetGenerateCommand::class,
101
                Console\OptimusGenerateCommand::class,
102
            ]);
103
        }
104 6
    }
105
}
106