MediaDataTable   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 73
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A query() 0 6 1
A ajax() 0 6 1
A getColumns() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\DataTables;
6
7
use Spatie\MediaLibrary\Models\Media;
8
use Cortex\Foundation\Transformers\MediaTransformer;
9
10
/**
11
 * @property \Spatie\MediaLibrary\HasMedia\HasMedia $resource
12
 * @property string                                 $tabs
13
 * @property string                                 $id
14
 * @property string                                 $url
15
 */
16
class MediaDataTable extends AbstractDataTable
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $model = Media::class;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected $createButton = false;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected $builderParameters = [
32
        'initComplete' => 'function (settings) {
33
            implicitForms.initialize();
34
        }',
35
    ];
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected $transformer = MediaTransformer::class;
41
42
    /**
43
     * Get the query object to be processed by dataTables.
44
     *
45
     * @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...
46
     */
47
    public function query()
48
    {
49
        $query = $this->resource->media()->orderBy('order_column', 'ASC');
50
51
        return $this->applyScopes($query);
52
    }
53
54
    /**
55
     * Display ajax response.
56
     *
57
     * @return \Illuminate\Http\JsonResponse
58
     */
59
    public function ajax()
60
    {
61
        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...
62
            ->setTransformer(app($this->transformer))
63
            ->make(true);
64
    }
65
66
    /**
67
     * Get columns.
68
     *
69
     * @return array
70
     */
71
    protected function getColumns(): array
72
    {
73
        return [
74
            'name' => ['title' => trans('cortex/foundation::common.name'), 'responsivePriority' => 0],
75
            'file_name' => ['title' => trans('cortex/foundation::common.file_name')],
76
            'mime_type' => ['title' => trans('cortex/foundation::common.mime_type')],
77
            'size' => ['title' => trans('cortex/foundation::common.size')],
78
            'created_at' => ['title' => trans('cortex/foundation::common.created_at'), 'render' => "moment(data).format('YYYY-MM-DD, hh:mm:ss A')"],
79
            'updated_at' => ['title' => trans('cortex/foundation::common.updated_at'), 'render' => "moment(data).format('YYYY-MM-DD, hh:mm:ss A')"],
80
            'delete' => ['title' => trans('cortex/foundation::common.delete'), 'orderable' => false, 'searchable' => false, 'render' => '"<a href=\"#\" data-toggle=\"modal\" data-target=\"#delete-confirmation\" 
81
                data-modal-action=\""+data+"\" 
82
                data-modal-title=\"" + Lang.trans(\'cortex/foundation::messages.delete_confirmation_title\') + "\"
83
                data-modal-button=\"<a href=\'#\' class=\'btn btn-danger\' data-form=\'delete\' data-token=\''.csrf_token().'\'><i class=\'fa fa-trash-o\'></i> \"" + Lang.trans(\'cortex/foundation::common.delete\') + "\"</a>\"
84
                data-modal-body=\"" + Lang.trans(\'cortex/foundation::messages.delete_confirmation_body\', {type: \'media\', name: full.name}) + "\"
85
                title=\"" + Lang.trans(\'cortex/foundation::common.delete\') + "\"><i class=\"fa fa-trash text-danger\"></i></a>"'],
86
        ];
87
    }
88
}
89