Completed
Push — master ( 7949b1...be02a5 )
by Nick
07:10
created

EloquentJsServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 1
A boot() 0 6 1
A enableGenericResourceRouting() 0 22 2
1
<?php
2
3
namespace EloquentJs\Laravel;
4
5
use EloquentJs\Laravel\Console\GenerateJavascript;
6
use EloquentJs\Laravel\RouteRegistrar;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\ServiceProvider;
10
use EloquentJs\Laravel\Controllers\GenericResourceController;
11
use EloquentJs\Laravel\Events\EloquentJsWasCalled;
12
use EloquentJs\Laravel\Listeners\ApplyQueryMethodsToBuilder;
13
use EloquentJs\Translators\JsonQueryTranslator;
14
use EloquentJs\Translators\QueryTranslator;
15
16
class EloquentJsServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Register the service provider.
20
     *
21
     * @return void
22
     */
23
    public function register()
24
    {
25
        $this->app->bind(QueryTranslator::class, function ($app) {
26
            return new JsonQueryTranslator($app['request']->input('query', '[]'));
27
        });
28
29
        $this->commands([GenerateJavascript::class]);
30
    }
31
32
    /**
33
     * Boot the service provider.
34
     *
35
     * @param Dispatcher $events
36
     * @param Router $router
37
     * @return void
38
     */
39
    public function boot(Dispatcher $events, Router $router)
40
    {
41
        $events->listen(EloquentJsWasCalled::class, ApplyQueryMethodsToBuilder::class);
42
43
        $this->enableGenericResourceRouting($router);
44
    }
45
46
    /**
47
     * Set up the generic resource routes + controller.
48
     *
49
     * @param Router $router
50
     * @return void
51
     */
52
    protected function enableGenericResourceRouting(Router $router)
53
    {
54
        $app = $this->app;
55
56
        $app->singleton('eloquentjs.router', function ($app) {
57
            return (new RouteRegistrar($app['router']))
58
                ->setController('\\'.GenericResourceController::class);
59
        });
60
61
        $router->macro('eloquent', function ($uri, $resource, $options = []) use ($app) {
62
            $app['eloquentjs.router']->addRoute($uri, $resource, $options);
63
        });
64
65
        // Typically you'd have dedicated controllers for each resource.
66
        // Since that's not the case here, we need some way of telling
67
        // our generic controller which resource we're working with.
68
        $app->resolving(function (GenericResourceController $controller, $app) {
0 ignored issues
show
Documentation introduced by
function (\EloquentJs\La...ke($resource)); } } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
            if ($resource = $app['eloquentjs.router']->getCurrentResource()) {
70
                $controller->setModel($app->make($resource));
71
            }
72
        });
73
    }
74
}
75