Passed
Push — master ( cc7a8f...5f1891 )
by Elf
06:09
created

HashidServiceProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 98.18%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
dl 0
loc 117
ccs 54
cts 55
cp 0.9818
rs 10
c 3
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerServices() 0 19 3
A registerCommands() 0 6 2
A getBindingKeyForDriver() 0 4 1
A getNonSingletonDrivers() 0 10 1
A setupAssets() 0 10 3
A register() 0 6 1
A getSingletonDrivers() 0 7 1
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;
1 ignored issue
show
Bug introduced by
The type Laravel\Lumen\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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