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

HashidServiceProvider::setupAssets()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 12
ccs 7
cts 9
cp 0.7778
crap 3.0987
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
     * 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