Completed
Push — master ( eb474e...621389 )
by Elf
01:22
created

HashidServiceProvider::createInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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