Completed
Push — master ( 3cfcc7...745194 )
by Elf
01:31
created

HashidServiceProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.98%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 121
ccs 53
cts 57
cp 0.9298
rs 10
wmc 12
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A registerCommands() 0 9 2
A setupAssets() 0 12 3
A getSingletonDrivers() 0 9 1
A getNonSingletonDrivers() 0 12 1
A getBindingKeyForDriver() 0 6 1
A registerServices() 0 21 3
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 6
        }
39 6
    }
40
41
    /**
42
     * Register service bindings.
43
     *
44
     * @return void
45
     */
46 6
    protected function registerServices()
47
    {
48
        $this->app->singleton('hashid', function ($app) {
49 4
            return new HashidManager($app);
50 6
        });
51 6
        $this->app->alias('hashid', HashidManager::class);
52
53 6
        foreach ($this->getSingletonDrivers() as $class) {
54 6
            $this->app->singleton(
55 6
                $key = $this->getBindingKeyForDriver($class),
56
                function () use ($class) {
57
                    return new $class;
58
                }
59 6
            );
60 6
            $this->app->alias($key, $class);
61 6
        }
62
63 6
        foreach ($this->getNonSingletonDrivers() as $class) {
64 6
            $this->app->bind($this->getBindingKeyForDriver($class), $class);
65 6
        }
66 6
    }
67
68
    /**
69
     * Get singleton drivers classes.
70
     *
71
     * @return array
72
     */
73 6
    protected function getSingletonDrivers()
74
    {
75
        return [
76 6
            Base64Driver::class,
77 6
            Base64IntegerDriver::class,
78 6
            HexDriver::class,
79 6
            HexIntegerDriver::class,
80 6
        ];
81
    }
82
83
    /**
84
     * Get non-singleton drivers classes.
85
     *
86
     * @return array
87
     */
88 6
    protected function getNonSingletonDrivers()
89
    {
90
        return [
91 6
            Base62Driver::class,
92 6
            Base62IntegerDriver::class,
93 6
            HashidsDriver::class,
94 6
            HashidsHexDriver::class,
95 6
            HashidsIntegerDriver::class,
96 6
            HashidsStringDriver::class,
97 6
            OptimusDriver::class,
98 6
        ];
99
    }
100
101
    /**
102
     * Get the binding key for the driver class.
103
     *
104
     * @param  string  $class
105
     * @return string
106
     */
107 6
    protected function getBindingKeyForDriver($class)
108
    {
109 6
        return 'hashid.driver.'.Str::snake(
110 6
            preg_replace('#Driver$#', '', class_basename($class))
111 6
        );
112
    }
113
114
    /**
115
     * Register console commands.
116
     *
117
     * @return void
118
     */
119 6
    protected function registerCommands()
120
    {
121 6
        if ($this->app->runningInConsole()) {
122 6
            $this->commands([
123 6
                Console\AlphabetGenerateCommand::class,
124 6
                Console\OptimusGenerateCommand::class,
125 6
            ]);
126 6
        }
127 6
    }
128
}
129