Completed
Push — master ( d01ebd...64f3e2 )
by Elf
13s
created

HashidServiceProvider::setupAssets()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 9.4285
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\ServiceProvider;
7
use Laravel\Lumen\Application as LumenApplication;
8
9
class HashidServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Register the service provider.
13
     *
14
     * @return void
15
     */
16 6
    public function register()
17
    {
18 6
        $this->setupAssets();
19
20 6
        $this->registerServices();
21 6
        $this->registerCommands();
22 6
    }
23
24
    /**
25
     * Setup package assets.
26
     *
27
     * @return void
28
     */
29 6
    protected function setupAssets()
30
    {
31 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...
32
            $this->app->configure('hashid');
33
        }
34
35 6
        $this->mergeConfigFrom($config = __DIR__.'/../config/hashid.php', 'hashid');
36
37 6
        if ($this->app->runningInConsole()) {
38 6
            $this->publishes([$config => base_path('config/hashid.php')], 'hashid');
39
        }
40 6
    }
41
42
    /**
43
     * Register service bindings.
44
     *
45
     * @return void
46
     */
47
    protected function registerServices()
48
    {
49 6
        $this->app->singleton('hashid', function ($app) {
50 4
            return new HashidManager($app);
51 6
        });
52 6
        $this->app->alias('hashid', HashidManager::class);
53
54 6
        foreach ($this->getSingletonDrivers() as $class) {
55 6
            $this->app->singleton(
56 6
                $key = $this->getBindingKeyForDriver($class),
57 6
                function () use ($class) {
58 1
                    return new $class;
59 6
                }
60
            );
61 6
            $this->app->alias($key, $class);
62
        }
63
64 6
        foreach ($this->getNonSingletonDrivers() as $class) {
65 6
            $this->app->bind($this->getBindingKeyForDriver($class), $class);
66
        }
67 6
    }
68
69
    /**
70
     * Get singleton drivers classes.
71
     *
72
     * @return array
73
     */
74 6
    protected function getSingletonDrivers()
75
    {
76
        return [
77 6
            Base64Driver::class,
78
            Base64IntegerDriver::class,
79
            HexDriver::class,
80
            HexIntegerDriver::class,
81
        ];
82
    }
83
84
    /**
85
     * Get non-singleton drivers classes.
86
     *
87
     * @return array
88
     */
89 6
    protected function getNonSingletonDrivers()
90
    {
91
        return [
92 6
            Base62Driver::class,
93
            Base62IntegerDriver::class,
94
            HashidsDriver::class,
95
            HashidsHexDriver::class,
96
            HashidsIntegerDriver::class,
97
            HashidsStringDriver::class,
98
            OptimusDriver::class,
99
        ];
100
    }
101
102
    /**
103
     * Get the binding key for the driver class.
104
     *
105
     * @param  string  $class
106
     * @return string
107
     */
108 6
    protected function getBindingKeyForDriver($class)
109
    {
110 6
        return 'hashid.driver.'.Str::snake(
111 6
            preg_replace('#Driver$#', '', class_basename($class))
112
        );
113
    }
114
115
    /**
116
     * Register console commands.
117
     *
118
     * @return void
119
     */
120 6
    protected function registerCommands()
121
    {
122 6
        if ($this->app->runningInConsole()) {
123 6
            $this->commands([
124 6
                Console\AlphabetGenerateCommand::class,
125
                Console\OptimusGenerateCommand::class,
126
            ]);
127
        }
128 6
    }
129
}
130