Completed
Push — master ( 0e1003...1276c7 )
by Abdelrahman
61:46 queued 59:06
created

ImportRecordsDataTable   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 2
cbo 2
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A query() 0 6 1
A ajax() 0 6 1
A getBuilderParameters() 0 38 1
A getColumns() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\DataTables;
6
7
use Cortex\Foundation\Models\ImportRecord;
8
use Cortex\Foundation\Transformers\ImportRecordTransformer;
9
10
/**
11
 * @property \Illuminate\Database\Eloquent\Model $resource
12
 * @property string                              $tabs
13
 * @property string                              $id
14
 * @property string                              $url
15
 */
16
class ImportRecordsDataTable extends AbstractDataTable
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $model = ImportRecord::class;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected $transformer = ImportRecordTransformer::class;
27
28
    /**
29
     * Get the query object to be processed by dataTables.
30
     *
31
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Illuminate\Database\Que...tabase\Eloquent\Builder.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
32
     */
33
    public function query()
34
    {
35
        $query = app($this->model)->query()->where('resource', $this->resource->getMorphClass());
36
37
        return $this->applyScopes($query);
38
    }
39
40
    /**
41
     * Display ajax response.
42
     *
43
     * @return \Illuminate\Http\JsonResponse
44
     */
45
    public function ajax()
46
    {
47
        return datatables($this->query())
0 ignored issues
show
Bug introduced by
The method setTransformer does only exist in Yajra\DataTables\DataTableAbstract, but not in Yajra\DataTables\DataTables.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
            ->setTransformer(app($this->transformer))
49
            ->make(true);
50
    }
51
52
    /**
53
     * Get default builder parameters.
54
     *
55
     * @return array
56
     */
57
    protected function getBuilderParameters(): array
58
    {
59
        $columnsButton = ['extend' => 'colvis', 'text' => '<i class="fa fa-columns"></i> '.trans('cortex/foundation::common.columns').' <span class="caret"/>'];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 160 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
60
        $lengthButton = ['extend' => 'pageLength', 'text' => '<i class="fa fa-list-ol"></i> '.trans('cortex/foundation::common.limit').' <span class="caret"/>'];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 161 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
61
        $importButton = ['extend' => 'import', 'action' => "function () {
62
    let selectedIds = [];
63
    let selected = this.rows( { selected: true } );
64
    if (selected.count()) {
65
        selected.data().each(function (row) {
66
            selectedIds.push(row.id);
67
        });
68
69
        $.ajax({
70
            method: 'POST',
71
            data: {
72
                selected_ids: selectedIds,
73
                _token: window.Laravel.csrfToken,
74
            },
75
            url: window.location.pathname.replace('/import', '/hoard'),
76
            success: function(response) {
77
                let notification = function() { $.notify({message: response}, {type: 'info', mouse_over: 'pause', z_index: 9999, animate:{enter: \"animated fadeIn\", exit: \"animated fadeOut\"}}); }; if (typeof notification === 'function') { notification(); notification = null; };
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 281 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
78
                window.location.reload();
79
            },
80
        });
81
    }}"];
82
83
        return array_merge([
84
            'dom' => $this->dom,
85
            'keys' => $this->keys,
86
            'mark' => $this->mark,
87
            'order' => $this->order,
88
            'select' => $this->select,
89
            'retrieve' => $this->retrieve,
90
            'autoWidth' => $this->autoWidth,
91
            'fixedHeader' => $this->fixedHeader,
92
            'buttons' => ['print', 'reset', 'reload', $importButton, $columnsButton, $lengthButton],
93
        ], $this->builderParameters);
94
    }
95
96
    /**
97
     * Get columns.
98
     *
99
     * @return array
100
     */
101
    protected function getColumns(): array
102
    {
103
        return [
104
            'resource' => ['title' => trans('cortex/foundation::common.resource')],
105
            'status' => ['title' => trans('cortex/foundation::common.status')],
106
            'data' => ['title' => trans('cortex/foundation::common.data'), 'orderable' => false],
107
            'created_at' => ['title' => trans('cortex/foundation::common.created_at'), 'render' => "moment(data).format('YYYY-MM-DD, hh:mm:ss A')"],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 148 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
108
            'updated_at' => ['title' => trans('cortex/foundation::common.updated_at'), 'render' => "moment(data).format('YYYY-MM-DD, hh:mm:ss A')"],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 148 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
109
        ];
110
    }
111
}
112