Completed
Push — master ( a6e8b1...08dd78 )
by Elf
01:49
created

HashidServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use ReflectionClass;
6
use Illuminate\Support\ServiceProvider;
7
use Laravel\Lumen\Application as LumenApplication;
8
9
class HashidServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Indicates if loading of the provider is deferred.
13
     *
14
     * @var bool
15
     */
16
    protected $defer = true;
17
18
    /**
19
     * Bootstrap the service provider.
20
     *
21
     * @return void
22
     */
23 6
    public function boot()
24
    {
25
        //
26 6
    }
27
28
    /**
29
     * Register the service provider.
30
     *
31
     * @return void
32
     */
33 6
    public function register()
34
    {
35 6
        $this->setupAssets();
36
37 6
        $this->registerServices();
38 6
        $this->registerCommands();
39 6
    }
40
41
    /**
42
     * Setup package assets.
43
     *
44
     * @return void
45
     */
46 6
    protected function setupAssets()
47
    {
48 6
        if ($this->app instanceof LumenApplication) {
0 ignored issues
show
Bug introduced by
The class Laravel\Lumen\Application does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

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