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

HashidServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
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