Completed
Push — master ( 1cd5f6...d6b401 )
by Arjay
03:58
created

FractalServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 5
dl 0
loc 91
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 7 1
A publishAssets() 0 6 1
A registerMacro() 0 14 1
A register() 0 22 2
A provides() 0 7 1
1
<?php
2
3
namespace Yajra\DataTables;
4
5
use Illuminate\Support\ServiceProvider;
6
use League\Fractal\Manager;
7
use League\Fractal\Serializer\DataArraySerializer;
8
use Yajra\DataTables\Transformers\FractalTransformer;
9
10
class FractalServiceProvider 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 the application events.
21
     *
22
     * @return void
23
     */
24
    public function boot()
25
    {
26
        $this->mergeConfigFrom(__DIR__ . '/config/config.php', 'datatables-fractal');
27
        $this->publishAssets();
28
29
        $this->registerMacro();
30
    }
31
32
    /**
33
     * Publish datatables assets.
34
     */
35
    protected function publishAssets()
36
    {
37
        $this->publishes([
38
            __DIR__ . '/config/config.php' => config_path('datatables-fractal.php'),
39
        ], 'datatables-fractal');
40
    }
41
42
    /**
43
     * Register DataTables macro methods.
44
     */
45
    protected function registerMacro()
46
    {
47
        DataTableAbstract::macro('setTransformer', function ($transformer) {
48
            $this->transformer = $transformer;
0 ignored issues
show
Bug introduced by
The property transformer 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...
49
50
            return $this;
51
        });
52
53
        DataTableAbstract::macro('setSerializer', function ($serializer) {
54
            $this->serializer = $serializer;
0 ignored issues
show
Bug introduced by
The property serializer 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...
55
56
            return $this;
57
        });
58
    }
59
60
    /**
61
     * Register the service provider.
62
     *
63
     * @return void
64
     */
65
    public function register()
66
    {
67
        $this->app->singleton('datatables.fractal', function () {
68
            $fractal = new Manager;
69
            $config  = $this->app['config'];
70
            $request = $this->app['request'];
71
72
            $includesKey = $config->get('datatables-fractal.includes', 'include');
73
            if ($request->get($includesKey)) {
74
                $fractal->parseIncludes($request->get($includesKey));
75
            }
76
77
            $serializer = $config->get('datatables-fractal.serializer', DataArraySerializer::class);
78
            $fractal->setSerializer(new $serializer);
79
80
            return $fractal;
81
        });
82
83
        $this->app->singleton('datatables.transformer', function () {
84
            return new FractalTransformer($this->app->make('datatables.fractal'));
85
        });
86
    }
87
88
    /**
89
     * Get the services provided by the provider.
90
     *
91
     * @return array
92
     */
93
    public function provides()
94
    {
95
        return [
96
            'datatables.fractal',
97
            'datatables.transformer',
98
        ];
99
    }
100
}
101