Completed
Push — master ( 95ebcc...c49337 )
by Elf
03:51
created

OptimusServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 81
ccs 0
cts 38
cp 0
rs 10
c 1
b 0
f 0
wmc 8
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
B extendRouter() 0 26 4
A register() 0 14 2
A provides() 0 4 1
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Services\Optimus;
4
5
use Closure;
6
use Jenssegers\Optimus\Optimus;
7
use Illuminate\Support\ServiceProvider;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
10
class OptimusServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Indicates if loading of the provider is deferred.
14
     *
15
     * @var bool
16
     */
17
    protected $defer = false;
18
19
    /**
20
     * Bootstrap any application services.
21
     *
22
     * @return void
23
     */
24
    public function boot()
25
    {
26
        $this->extendRouter();
27
    }
28
29
    /**
30
     * Register `modelWithOptimusHashid` router macro.
31
     *
32
     * @return void
33
     */
34
    protected function extendRouter()
35
    {
36
        $router = $this->app->make('router');
37
38
        $router->macro('modelWithOptimusHashid',
39
            function ($key, $class, Closure $callback = null) use ($router) {
40
                $router->bind($key, function ($value) use ($router, $class, $callback) {
41
                    if (is_null($value)) {
42
                        return;
43
                    }
44
45
                    $instance = $router->container->make($class);
46
47
                    if ($model = $instance->where($instance->getRouteKeyName(), $instance->getKeyForHashid($value))->first()) {
48
                        return $model;
49
                    }
50
51
                    if ($callback instanceof Closure) {
52
                        return call_user_func($callback, $value);
53
                    }
54
55
                    throw (new ModelNotFoundException)->setModel($class);
56
                });
57
            }
58
        );
59
    }
60
61
    /**
62
     * Register the service provider.
63
     *
64
     * @return void
65
     */
66
    public function register()
67
    {
68
        $this->app->singleton('optimus', function ($app) {
69
            $config = $app['config']->get('support.optimus');
70
71
            return new Optimus($config['prime'], $config['inverse'], $config['random']);
72
        });
73
74
        $this->app->alias('optimus', Optimus::class);
75
76
        if ($this->app->runningInConsole()) {
77
            $this->commands(GenerateOptimusCommand::class);
78
        }
79
    }
80
81
    /**
82
     * Get the services provided by the provider.
83
     *
84
     * @return array
85
     */
86
    public function provides()
87
    {
88
        return ['optimus', Optimus::class];
89
    }
90
}
91