Completed
Push — develop ( 3aebdd...e491f1 )
by Abdelrahman
02:10
created

ImportRecordsDataTable   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 2
cbo 1
dl 0
loc 99
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
B getBuilderParameters() 0 41 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
class ImportRecordsDataTable extends AbstractDataTable
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    protected $model = ImportRecord::class;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected $transformer = ImportRecordTransformer::class;
21
22
    /**
23
     * Get the query object to be processed by dataTables.
24
     *
25
     * @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...
26
     */
27
    public function query()
28
    {
29
        $query = app($this->model)->query()->where('resource', $this->resource->getMorphClass());
0 ignored issues
show
Documentation introduced by
The property resource does not exist on object<Cortex\Foundation...ImportRecordsDataTable>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
30
31
        return $this->applyScopes($query);
32
    }
33
34
    /**
35
     * Display ajax response.
36
     *
37
     * @return \Illuminate\Http\JsonResponse
38
     */
39
    public function ajax()
40
    {
41
        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...
42
            ->setTransformer(app($this->transformer))
43
            ->make(true);
44
    }
45
46
    /**
47
     * Get default builder parameters.
48
     *
49
     * @return array
50
     */
51
    protected function getBuilderParameters(): array
52
    {
53
        $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...
54
        $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...
55
        $importButton = ['extend' => 'import', 'action' => "function () {
56
    let selectedIds = [];
57
    let selected = this.rows( { selected: true } );
58
    if (selected.count()) {
59
        selected.data().each(function (row) {
60
        console.log(row);
61
            selectedIds.push(row.id);
62
        });
63
64
        $.ajax({
65
            method: 'POST',
66
            data: {
67
                selected_ids: selectedIds,
68
                _token: window.Laravel.csrfToken,
69
            },
70
            url: window.location.pathname.replace('/import', '/hoard'),
71
            success: function(response) {
72
                let notification = function() { $.notify({message: response}, {type: 'info', mouse_over: 'pause', z_index: 9999, animate:{enter: \"animated fadeIn\", exit: \"animated fadeOut\"}}); }; console.log('asd'); if (typeof notification === 'function') { notification(); notification = null; };
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 301 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...
73
                window.location.reload();
74
            },
75
        });
76
    }}"];
77
78
        return array_merge([
79
            'select' => true,
80
            'stateSave' => true,
81
            'dom' => $this->dom,
82
            'keys' => false,
83
            'mark' => $this->mark,
84
            'order' => $this->order,
85
            'retrieve' => $this->retrieve,
86
            'autoWidth' => $this->autoWidth,
87
            'searchPane' => $this->searchPane,
88
            'fixedHeader' => $this->fixedHeader,
89
            'buttons' => ['print', 'reset', 'reload', $importButton, $columnsButton, $lengthButton],
90
        ], $this->builderParameters);
91
    }
92
93
    /**
94
     * Get columns.
95
     *
96
     * @return array
97
     */
98
    protected function getColumns(): array
99
    {
100
        return [
101
            'resource' => ['title' => trans('cortex/foundation::common.resource')],
102
            'status' => ['title' => trans('cortex/foundation::common.status')],
103
            'data' => ['title' => trans('cortex/foundation::common.data'), 'orderable' => false],
104
            '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...
105
            '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...
106
        ];
107
    }
108
}
109