Completed
Push — master ( 428a52...76514c )
by Elf
01:37
created

HashidServiceProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 98.25%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 136
ccs 56
cts 57
cp 0.9825
rs 10
c 2
b 0
f 0
wmc 14
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A registerServices() 0 14 3
A createInstance() 0 7 2
A getSingletonBindings() 0 10 1
A getClassAliases() 0 11 1
A registerCommands() 0 9 2
A provides() 0 7 1
A setupAssets() 0 12 3
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
     * Register the service provider.
19
     *
20
     * @return void
21
     */
22 7
    public function register()
23
    {
24 7
        $this->setupAssets();
25
26 6
        $this->registerServices();
27 6
        $this->registerCommands();
28 6
    }
29
30
    /**
31
     * Setup package assets.
32
     *
33
     * @return void
34
     */
35 7
    protected function setupAssets()
36
    {
37 7
        if (is_a($this->app, 'Laravel\Lumen\Application')) {
38 1
            $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...
39
        }
40
41 6
        $this->mergeConfigFrom($config = __DIR__.'/../config/hashid.php', 'hashid');
42
43 6
        if ($this->app->runningInConsole()) {
44 6
            $this->publishes([$config => config_path('hashid.php')], 'hashid');
45 6
        }
46 6
    }
47
48
    /**
49
     * Register service bindings.
50
     *
51
     * @return void
52
     */
53 6
    protected function registerServices()
54
    {
55 6
        foreach ($this->getSingletonBindings() as $abstract => $concrete) {
56 6
            $this->app->singleton($abstract, function ($app) use ($concrete) {
57 4
                return $this->createInstance($concrete, [$app]);
58 6
            });
59
60 6
            $this->app->alias($abstract, $concrete);
61 6
        }
62
63 6
        foreach ($this->getClassAliases() as $abstract => $alias) {
64 6
            $this->app->alias($abstract, $alias);
65 6
        }
66 6
    }
67
68
    /**
69
     * Create a new instance from class name.
70
     *
71
     * @param  string  $class
72
     * @param  array  $args
73
     * @return mixed
74
     */
75 4
    protected function createInstance($class, array $args = [])
76
    {
77 4
        $reflector = new ReflectionClass($class);
78
79 4
        return is_null($reflector->getConstructor())
80 4
            ? new $class : $reflector->newInstanceArgs($args);
81
    }
82
83
    /**
84
     * Get singleton bindings to be registered.
85
     *
86
     * @return array
87
     */
88 6
    protected function getSingletonBindings()
89
    {
90
        return [
91 6
            'hashid' => HashidManager::class,
92 6
            'hashid.driver.base64' => Base64Driver::class,
93 6
            'hashid.driver.base64.integer' => Base64IntegerDriver::class,
94 6
            'hashid.driver.hex' => HexDriver::class,
95 6
            'hashid.driver.hex.integer' => HexIntegerDriver::class,
96 6
        ];
97
    }
98
99
    /**
100
     * Get class aliases to be registered.
101
     *
102
     * @return array
103
     */
104 6
    protected function getClassAliases()
105
    {
106
        return [
107 6
            Base62Driver::class => 'hashid.driver.base62',
108 6
            Base62IntegerDriver::class => 'hashid.driver.base62.integer',
109 6
            HashidsDriver::class => 'hashid.driver.hashids',
110 6
            HashidsHexDriver::class => 'hashid.driver.hashids.hex',
111 6
            HashidsIntegerDriver::class => 'hashid.driver.hashids.integer',
112 6
            OptimusDriver::class => 'hashid.driver.optimus',
113 6
        ];
114
    }
115
116
    /**
117
     * Register console commands.
118
     *
119
     * @return void
120
     */
121 6
    protected function registerCommands()
122
    {
123 6
        if ($this->app->runningInConsole()) {
124 6
            $this->commands([
125 6
                Console\AlphabetGenerateCommand::class,
126 6
                Console\OptimusGenerateCommand::class,
127 6
            ]);
128 6
        }
129 6
    }
130
131
    /**
132
     * Get the services provided by the provider.
133
     *
134
     * @return string[]
135
     */
136 2
    public function provides()
137
    {
138 2
        return array_merge(
139 2
            array_keys($bindings = $this->getSingletonBindings()),
140 2
            array_values($bindings)
141 2
        );
142
    }
143
}
144