Passed
Push — master ( 62919d...1f65fa )
by Iman
04:23
created

Deleter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _redirectBackWithMsg() 0 12 4
A getDeleteImage() 0 16 1
A postActionSelected() 0 14 3
A getDelete() 0 6 2
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\controllers\CBController;
4
5
use Crocodicstudio\Crudbooster\CBCoreModule\DataRemover;
6
use Illuminate\Support\Facades\Request;
7
use Illuminate\Support\Facades\Storage;
8
use Crocodicstudio\Crudbooster\helpers\CRUDBooster;
9
10
trait Deleter
11
{
12
    public function getDelete($id)
13
    {
14
        $this->genericLoader();
0 ignored issues
show
Bug introduced by
It seems like genericLoader() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
        $this->/** @scrutinizer ignore-call */ 
15
               genericLoader();
Loading history...
15
        (new DataRemover($this))->doDeleteWithHook([$id]);
16
        $url = request('return_url') ?: Request::server('HTTP_REFERER');
17
        CRUDBooster::redirect($url, cbTrans('alert_delete_data_success'), 'success');
18
    }
19
20
    public function postActionSelected()
21
    {
22
        $this->genericLoader();
23
        $selectedIds = request('checkbox');
24
        $btnName = request('button_name');
25
        if (! $selectedIds) {
26
            backWithMsg(cbTrans('at_least_one_row'), 'warning');
27
        }
28
        if ($btnName == 'delete') {
29
            (new DataRemover($this))->doDeleteWithHook($selectedIds);
30
            backWithMsg(cbTrans('alert_delete_selected_success'));
31
        }
32
33
        return $this->_redirectBackWithMsg($btnName, $selectedIds);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_redirectBackWith...$btnName, $selectedIds) targeting Crocodicstudio\Crudboost...:_redirectBackWithMsg() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34
    }
35
36
    /**
37
     * @param $button_name
38
     * @param $id_selected
39
     * @return null
40
     */
41
    private function _redirectBackWithMsg($button_name, $id_selected)
42
    {
43
        $action = ucwords(str_replace(['-', '_'], ' ', $button_name));
44
        $type = 'success';
45
        $message = cbTrans('alert_action', ['action' => $action]);
46
47
        if ($this->actionButtonSelected($id_selected, $button_name) === false) {
0 ignored issues
show
Bug introduced by
It seems like actionButtonSelected() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        if ($this->/** @scrutinizer ignore-call */ actionButtonSelected($id_selected, $button_name) === false) {
Loading history...
48
            $message = ! empty($this->alert['message']) ? $this->alert['message'] : 'Error';
49
            $type = ! empty($this->alert['type']) ? $this->alert['type'] : 'danger';
50
        }
51
52
        backWithMsg($message, $type);
53
    }
54
55
    public function getDeleteImage()
56
    {
57
        $this->genericLoader();
58
        $id = (int) request('id');
59
        $column = request('column');
60
61
        $row = $this->findRow($id)->first();
0 ignored issues
show
Bug introduced by
It seems like findRow() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        $row = $this->/** @scrutinizer ignore-call */ findRow($id)->first();
Loading history...
62
        $file = $row->{$column};
63
64
        Storage::delete($file);
65
66
        $this->findRow($id)->update([$column => null]);
67
68
        event('cb.imageDeleted', [$this->table, compact('id', 'column', 'file'), YmdHis(), cbUser()]);
69
70
        backWithMsg(cbTrans('alert_delete_data_success'));
71
    }
72
}