Completed
Push — master ( d44f60...9b69eb )
by Propa
06:56 queued 03:45
created

FakeIdServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 86.49%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 9
c 8
b 1
f 0
lcom 1
cbo 5
dl 0
loc 88
ccs 32
cts 37
cp 0.8649
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 7 1
B register() 0 25 1
C registerRouterMacro() 0 37 7
1
<?php namespace Propaganistas\LaravelFakeId;
2
3
use Closure;
4
use Illuminate\Support\ServiceProvider;
5
use Jenssegers\Optimus\Optimus;
6
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
7
8
class FakeIdServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Boots the service provider.
12
     *
13
     * @return void
14
     */
15 24
    public function boot()
16
    {
17
        // Publish config.
18 24
        $this->publishes([
19 24
            __DIR__ . '/../config/config.php' => config_path('fakeid.php'),
20 24
        ], 'config');
21 24
    }
22
23
    /**
24
     * Register the service provider.
25
     *
26
     * @return void
27
     */
28 24
    public function register()
29
    {
30
        // Merge default config.
31 24
        $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'fakeid');
32
33
        // Register setup command.
34 16
        $this->app->singleton('fakeid.command.setup', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
            return new Commands\FakeIdSetupCommand();
36 24
        });
37 24
        $this->commands('fakeid.command.setup');
38
39
        // Register FakeId driver.
40 16
        $this->app->singleton('Jenssegers\Optimus\Optimus', function ($app) {
41 18
            return new Optimus(
42 18
                $app['config']['fakeid.prime'],
43 18
                $app['config']['fakeid.inverse'],
44 18
                $app['config']['fakeid.random']
45
            );
46 24
        });
47
48 24
        $this->app->alias('Jenssegers\Optimus\Optimus', 'optimus');
49 24
        $this->app->alias('Jenssegers\Optimus\Optimus', 'fakeid');
50
51 24
        $this->registerRouterMacro();
52 24
    }
53
54
    /**
55
     * Register the custom router macro.
56
     */
57
    protected function registerRouterMacro()
58
    {
59
        $this->app['router']->macro('fakeIdModel', function ($key, $class, Closure $callback = null) {
60 24
            $this->bind($key, function ($value) use ($key, $class, $callback) {
0 ignored issues
show
Bug introduced by
The method bind() does not seem to exist on object<Propaganistas\Lar...\FakeIdServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61 9
                if (is_null($value)) {
62
                    return;
63
                }
64
65
                // For model binders, we will attempt to retrieve the models using the first
66
                // method on the model instance. If we cannot retrieve the models we'll
67
                // throw a not found exception otherwise we will return the instance.
68 9
                $instance = $this->container->make($class);
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
70
                // Decode FakeId first if applicable.
71 9
                if (in_array('Propaganistas\LaravelFakeId\FakeIdTrait', class_uses_recursive($class))) {
72
                    try {
73 9
                        $value = $this->container->make('fakeid')->decode($value);
74 6
                    } catch (\InvalidArgumentException $e) {
75 6
                        throw config('app.debug') ? $e : new NotFoundHttpException;
76
                    }
77
                }
78
79 3
                if ($model = $instance->where($instance->getRouteKeyName(), $value)->first()) {
80 3
                    return $model;
81
                }
82
83
                // If a callback was supplied to the method we will call that to determine
84
                // what we should do when the model is not found. This just gives these
85
                // developer a little greater flexibility to decide what will happen.
86
                if ($callback instanceof Closure) {
87
                    return call_user_func($callback, $value);
88
                }
89
90
                throw new NotFoundHttpException;
91 24
            });
92 16
        });
93 16
    }
94
95
}
96