Completed
Push — master ( 276aee...df87e1 )
by Elf
02:01
created

HashidServiceProvider::createInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use ReflectionClass;
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->getSingletonBindings() as $abstract => $concrete) {
54
            $this->app->singleton($abstract, function ($app) use ($concrete) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
                return new $concrete;
56 6
            });
57 6
            $this->app->alias($abstract, $concrete);
58 6
        }
59
60 6
        foreach ($this->getAliasesBindings() as $abstract => $concrete) {
61 6
            $this->app->bind($abstract, $concrete);
62 6
        }
63
64 6
        $this->app->bind('hashid.connection', function ($app) {
65 1
            return $app['hashid']->connection();
66 6
        });
67 6
    }
68
69
    /**
70
     * Get singleton bindings to be registered.
71
     *
72
     * @return array
73
     */
74 6
    protected function getSingletonBindings()
75
    {
76
        return [
77 6
            'hashid.driver.base64' => Base64Driver::class,
78 6
            'hashid.driver.base64.integer' => Base64IntegerDriver::class,
79 6
            'hashid.driver.hex' => HexDriver::class,
80 6
            'hashid.driver.hex.integer' => HexIntegerDriver::class,
81 6
        ];
82
    }
83
84
    /**
85
     * Get class aliases to be registered.
86
     *
87
     * @return array
88
     */
89 6
    protected function getAliasesBindings()
90
    {
91
        return [
92 6
            'hashid.driver.base62' => Base62Driver::class,
93 6
            'hashid.driver.base62.integer' => Base62IntegerDriver::class,
94 6
            'hashid.driver.hashids' => HashidsDriver::class,
95 6
            'hashid.driver.hashids.hex' => HashidsHexDriver::class,
96 6
            'hashid.driver.hashids.integer' => HashidsIntegerDriver::class,
97 6
            'hashid.driver.hashids.string' => HashidsStringDriver::class,
98 6
            'hashid.driver.optimus' => OptimusDriver::class,
99 6
        ];
100
    }
101
102
    /**
103
     * Register console commands.
104
     *
105
     * @return void
106
     */
107 6
    protected function registerCommands()
108
    {
109 6
        if ($this->app->runningInConsole()) {
110 6
            $this->commands([
111 6
                Console\AlphabetGenerateCommand::class,
112 6
                Console\OptimusGenerateCommand::class,
113 6
            ]);
114 6
        }
115 6
    }
116
}
117