Completed
Push — master ( 876648...f3fdac )
by Elf
01:17
created

HashidServiceProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.36%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 121
ccs 53
cts 55
cp 0.9636
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A createInstance() 0 7 2
A getSingletonBindings() 0 10 1
A registerServices() 0 18 3
A getClassAliases() 0 12 1
A registerCommands() 0 9 2
A setupAssets() 0 12 3
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
     * Register the service provider.
12
     *
13
     * @return void
14
     */
15 6
    public function register()
16
    {
17 6
        $this->setupAssets();
18
19 6
        $this->registerServices();
20 6
        $this->registerCommands();
21 6
    }
22
23
    /**
24
     * Setup package assets.
25
     *
26
     * @return void
27
     */
28 6
    protected function setupAssets()
29
    {
30 6
        if (is_a($this->app, 'Laravel\Lumen\Application')) {
31
            $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...
32
        }
33
34 6
        $this->mergeConfigFrom($config = __DIR__.'/../config/hashid.php', 'hashid');
35
36 6
        if ($this->app->runningInConsole()) {
37 6
            $this->publishes([$config => base_path('config/hashid.php')], 'hashid');
38 6
        }
39 6
    }
40
41
    /**
42
     * Register service bindings.
43
     *
44
     * @return void
45
     */
46 6
    protected function registerServices()
47
    {
48 6
        foreach ($this->getSingletonBindings() as $abstract => $concrete) {
49
            $this->app->singleton($abstract, function ($app) use ($concrete) {
50 4
                return $this->createInstance($concrete, [$app]);
51 6
            });
52
53 6
            $this->app->alias($abstract, $concrete);
54 6
        }
55
56 6
        foreach ($this->getClassAliases() as $abstract => $alias) {
57 6
            $this->app->alias($abstract, $alias);
58 6
        }
59
60 6
        $this->app->bind('hashid.connection', function ($app) {
61 1
            return $app['hashid']->connection();
62 6
        });
63 6
    }
64
65
    /**
66
     * Create a new instance from class name.
67
     *
68
     * @param  string  $class
69
     * @param  array  $args
70
     * @return mixed
71
     */
72 4
    protected function createInstance($class, array $args = [])
73
    {
74 4
        $reflector = new ReflectionClass($class);
75
76 4
        return is_null($reflector->getConstructor())
77 4
            ? new $class : $reflector->newInstanceArgs($args);
78
    }
79
80
    /**
81
     * Get singleton bindings to be registered.
82
     *
83
     * @return array
84
     */
85 6
    protected function getSingletonBindings()
86
    {
87
        return [
88 6
            'hashid' => HashidManager::class,
89 6
            'hashid.driver.base64' => Base64Driver::class,
90 6
            'hashid.driver.base64.integer' => Base64IntegerDriver::class,
91 6
            'hashid.driver.hex' => HexDriver::class,
92 6
            'hashid.driver.hex.integer' => HexIntegerDriver::class,
93 6
        ];
94
    }
95
96
    /**
97
     * Get class aliases to be registered.
98
     *
99
     * @return array
100
     */
101 6
    protected function getClassAliases()
102
    {
103
        return [
104 6
            Base62Driver::class => 'hashid.driver.base62',
105 6
            Base62IntegerDriver::class => 'hashid.driver.base62.integer',
106 6
            HashidsDriver::class => 'hashid.driver.hashids',
107 6
            HashidsHexDriver::class => 'hashid.driver.hashids.hex',
108 6
            HashidsIntegerDriver::class => 'hashid.driver.hashids.integer',
109 6
            HashidsStringDriver::class => 'hashid.driver.hashids.string',
110 6
            OptimusDriver::class => 'hashid.driver.optimus',
111 6
        ];
112
    }
113
114
    /**
115
     * Register console commands.
116
     *
117
     * @return void
118
     */
119 6
    protected function registerCommands()
120
    {
121 6
        if ($this->app->runningInConsole()) {
122 6
            $this->commands([
123 6
                Console\AlphabetGenerateCommand::class,
124 6
                Console\OptimusGenerateCommand::class,
125 6
            ]);
126 6
        }
127 6
    }
128
}
129