Completed
Push — master ( 7fe252...a749dd )
by Elf
01:47
created

HashidServiceProvider::getNonSingletonDrivers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
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
    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 6
        foreach ($this->getSingletonDrivers() as $class) {
54 6
            $this->app->singleton($key = $this->getBindingKeyForDriver($class), $class);
55 6
            $this->app->alias($key, $class);
56 6
        }
57
58 6
        foreach ($this->getNonSingletonDrivers() as $class) {
59 6
            $this->app->bind($this->getBindingKeyForDriver($class), $class);
60 6
        }
61 6
    }
62
63
    /**
64
     * Get singleton drivers classes.
65
     *
66
     * @return array
67
     */
68 6
    protected function getSingletonDrivers()
69
    {
70
        return [
71 6
            Base64Driver::class,
72 6
            Base64IntegerDriver::class,
73 6
            HexDriver::class,
74 6
            HexIntegerDriver::class,
75 6
        ];
76
    }
77
78
    /**
79
     * Get non-singleton drivers classes.
80
     *
81
     * @return array
82
     */
83 6
    protected function getNonSingletonDrivers()
84
    {
85
        return [
86 6
            Base62Driver::class,
87 6
            Base62IntegerDriver::class,
88 6
            HashidsDriver::class,
89 6
            HashidsHexDriver::class,
90 6
            HashidsIntegerDriver::class,
91 6
            HashidsStringDriver::class,
92 6
            OptimusDriver::class,
93 6
        ];
94
    }
95
96
    /**
97
     * Get the binding key for the driver class.
98
     *
99
     * @param  string  $class
100
     * @return string
101
     */
102 6
    protected function getBindingKeyForDriver($class)
103
    {
104 6
        return 'hashid.driver.'.Str::snake(
105 6
            preg_replace('#Driver$#', '', class_basename($class))
106 6
        );
107
    }
108
109
    /**
110
     * Register console commands.
111
     *
112
     * @return void
113
     */
114 6
    protected function registerCommands()
115
    {
116 6
        if ($this->app->runningInConsole()) {
117 6
            $this->commands([
118 6
                Console\AlphabetGenerateCommand::class,
119 6
                Console\OptimusGenerateCommand::class,
120 6
            ]);
121 6
        }
122 6
    }
123
}
124