Completed
Push — master ( 389725...70dde1 )
by Propa
03:15
created

FakeIdServiceProvider::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 1
Metric Value
c 5
b 3
f 1
dl 0
loc 23
rs 9.0856
cc 1
eloc 11
nc 1
nop 0
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
	/**
12
	 * Indicates if loading of the provider is deferred.
13
	 *
14
	 * @var bool
15
	 */
16
	protected $defer = false;
17
18
	/**
19
	 * Boots the service provider.
20
	 *
21
	 * @return void
22
	 */
23
	public function boot()
24
	{
25
		// Publish config.
26
		$this->publishes([
27
			__DIR__ . '/../config/config.php' => config_path('fakeid.php'),
28
		]);
29
	}
30
31
	/**
32
	 * Register the service provider.
33
	 *
34
	 * @return void
35
	 */
36
	public function register()
37
	{
38
		// Merge default config.
39
		$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'fakeid');
40
41
		// Register setup command.
42
		$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...
43
			return new Commands\FakeIdSetupCommand();
44
		});
45
		$this->commands('fakeid.command.setup');
46
47
		// Register FakeId driver.
48
		$this->app->singleton('fakeid', function($app) {
49
			return new Optimus(
50
				$app['config']['fakeid.prime'],
51
				$app['config']['fakeid.inverse'],
52
				$app['config']['fakeid.random']
53
			);
54
		});
55
56
		$this->registerRouterMacro();
57
58
	}
59
60
	/**
61
	 * Get the services provided by the provider.
62
	 *
63
	 * @return array
64
	 */
65
	public function provides()
66
	{
67
		return [
68
			'fakeid',
69
			'fakeid.command.setup'
70
		];
71
	}
72
73
	/**
74
	 * Register the custom router macro.
75
	 */
76
	protected function registerRouterMacro()
77
	{
78
		$this->app['router']->macro('fakeIdModel', function($key, $class, Closure $callback = null) {
79
			$this->bind($key, function($value) use ($key, $class, $callback) {
0 ignored issues
show
Documentation Bug introduced by
The method bind does not exist on object<Propaganistas\Lar...\FakeIdServiceProvider>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
				if (is_null($value)) {
81
					return;
82
				}
83
84
				// For model binders, we will attempt to retrieve the models using the first
85
				// method on the model instance. If we cannot retrieve the models we'll
86
				// throw a not found exception otherwise we will return the instance.
87
				$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...
88
89
				// Decode FakeId first if applicable.
90
				if (in_array(FakeIdTrait::class, class_uses($class))) {
91
					$value = $this->container->make('fakeid')->decode($value);
92
				}
93
94
				if ($model = $instance->where($instance->getRouteKeyName(), $value)->first()) {
95
					return $model;
96
				}
97
98
				// If a callback was supplied to the method we will call that to determine
99
				// what we should do when the model is not found. This just gives these
100
				// developer a little greater flexibility to decide what will happen.
101
				if ($callback instanceof Closure) {
102
					return call_user_func($callback, $value);
103
				}
104
105
				throw new NotFoundHttpException;
106
			});
107
		});
108
	}
109
110
}
111