Completed
Push — master ( 6626dc...4677af )
by Elf
02:10
created

HashidServiceProvider   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 148
ccs 57
cts 60
cp 0.95
rs 10
c 2
b 0
f 0
wmc 15
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A register() 0 7 1
A setupAssets() 0 12 3
A createInstance() 0 10 2
A getSingletonBindings() 0 10 1
A registerServices() 0 14 3
A getClassAliases() 0 10 1
A registerCommands() 0 9 2
A provides() 0 7 1
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
     * Indicates if loading of the provider is deferred.
12
     *
13
     * @var bool
14
     */
15
    protected $defer = true;
16
17
    /**
18
     * Bootstrap the service provider.
19
     *
20
     * @return void
21
     */
22 7
    public function boot()
23
    {
24
        //
25 7
    }
26
27
    /**
28
     * Register the service provider.
29
     *
30
     * @return void
31
     */
32 7
    public function register()
33
    {
34 7
        $this->setupAssets();
35
36 7
        $this->registerServices();
37 7
        $this->registerCommands();
38 7
    }
39
40
    /**
41
     * Setup package assets.
42
     *
43
     * @return void
44
     */
45 7
    protected function setupAssets()
46
    {
47 7
        if (is_a($this->app, 'Laravel\Lumen\Application')) {
48
            $this->app->configure('hashid');
0 ignored issues
show
Bug introduced by
The method configure() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean registerConfiguredProviders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
        }
50
51 7
        $this->mergeConfigFrom($config = __DIR__.'/../config/hashid.php', 'hashid');
52
53 7
        if ($this->app->runningInConsole()) {
54 7
            $this->publishes([$config => config_path('hashid.php')], 'hashid');
55 7
        }
56 7
    }
57
58
    /**
59
     * Register service bindings.
60
     *
61
     * @return void
62
     */
63 7
    protected function registerServices()
64 7
    {
65 7
        foreach ($this->getSingletonBindings() as $abstract => $concrete) {
66 7
            $this->app->singleton($abstract, function ($app) use ($concrete) {
67 5
                return $this->createInstance($concrete, [$app]);
68 7
            });
69
70 7
            $this->app->alias($abstract, $concrete);
71 7
        }
72
73 7
        foreach ($this->getClassAliases() as $abstract => $alias) {
74 7
            $this->app->alias($abstract, $alias);
75 7
        }
76 7
    }
77
78
    /**
79
     * Create a new instance from class name.
80
     *
81
     * @param  string  $class
82
     * @param  array  $args
83
     * @return mixed
84
     */
85 5
    protected function createInstance($class, array $args = [])
86
    {
87 5
        $reflector = new ReflectionClass($class);
88
89 5
        if (is_null($reflector->getConstructor())) {
90
            return new $class;
91
        }
92
93 5
        return $reflector->newInstanceArgs($args);
94
    }
95
96
    /**
97
     * Get singleton bindings to be registered.
98
     *
99
     * @return array
100
     */
101 7
    protected function getSingletonBindings()
102
    {
103
        return [
104 7
            'hashid' => HashidManager::class,
105 7
            'hashid.driver.base64' => Base64Driver::class,
106 7
            'hashid.driver.base64.integer' => Base64IntegerDriver::class,
107 7
            'hashid.driver.hex' => HexDriver::class,
108 7
            'hashid.driver.hex.integer' => HexIntegerDriver::class,
109 7
        ];
110
    }
111
112
    /**
113
     * Get class aliases to be registered.
114
     *
115
     * @return array
116
     */
117 7
    protected function getClassAliases()
118
    {
119
        return [
120 7
            Base62Driver::class => 'hashid.driver.base62',
121 7
            Base62IntegerDriver::class => 'hashid.driver.base62.integer',
122 7
            HashidsDriver::class => 'hashid.driver.hashids',
123 7
            HashidsHexDriver::class => 'hashid.driver.hashids.hex',
124 7
            OptimusDriver::class => 'hashid.driver.optimus',
125 7
        ];
126
    }
127
128
    /**
129
     * Register console commands.
130
     *
131
     * @return void
132
     */
133 7
    protected function registerCommands()
134
    {
135 7
        if ($this->app->runningInConsole()) {
136 7
            $this->commands([
137 7
                Console\AlphabetGenerateCommand::class,
138 7
                Console\OptimusGenerateCommand::class,
139 7
            ]);
140 7
        }
141 7
    }
142
143
    /**
144
     * Get the services provided by the provider.
145
     *
146
     * @return string[]
147
     */
148 3
    public function provides()
149
    {
150 3
        return array_merge(
151 3
            array_keys($bindings = $this->getSingletonBindings()),
152 3
            array_values($bindings)
153 3
        );
154
    }
155
}
156