Completed
Push — master ( d10f05...cc7a8f )
by Elf
01:17
created

HashidServiceProvider::setupAssets()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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