Completed
Push — master ( 745194...d05240 )
by Elf
01:40
created

HashidServiceProvider::getSingletonDrivers()   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
nc 1
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 9.6666
c 1
b 0
f 0
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 6
                function () use ($class) {
57 6
                    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