Completed
Push — master ( 188ffb...31684b )
by Maxime
02:48
created

ModelBaseController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 103
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A putDestroy() 0 14 2
A postSearch() 0 19 3
A generateResultSearchByIds() 0 12 3
A generateResultSearch() 0 20 2
A getParams() 0 10 2
1
<?php namespace Distilleries\Expendable\Http\Controllers\Backend\Base;
2
3
use Distilleries\Expendable\Contracts\LayoutManagerContract;
4
use Distilleries\Expendable\Helpers\TranslationUtils;
5
use Distilleries\Expendable\Models\BaseModel;
6
use Illuminate\Http\Request;
7
8
class ModelBaseController extends BaseController {
9
10
    /**
11
     * @var \Distilleries\Expendable\Models\BaseModel $model
12
     * Injected by the constructor
13
     */
14
    protected $model;
15
16
17
    // ------------------------------------------------------------------------------------------------
18
19
    public function __construct(BaseModel $model, LayoutManagerContract $layoutManager)
20
    {
21
        parent::__construct($layoutManager);
22
        $this->model = $model;
23
    }
24
25
    // ------------------------------------------------------------------------------------------------
26
    // ------------------------------------------------------------------------------------------------
27
    // ------------------------------------------------------------------------------------------------
28
29
    public function putDestroy(Request $request)
30
    {
31
        $validation = \Validator::make($request->all(), [
32
            'id' => 'required'
33
        ]);
34
        if ($validation->fails()) {
35
            return redirect()->back()->withErrors($validation)->withInput($request->all());
36
        }
37
38
        $data = $this->model->where($this->model->getKeyName(), $request->get('id'))->get()->last();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Distilleries\Expendable\Models\BaseModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
39
        $data->delete();
40
41
        return redirect()->to(action('\\' . get_class($this) . '@getIndex'));
42
    }
43
44
    // ------------------------------------------------------------------------------------------------
45
    public function postSearch(Request $request, $query = null)
46
    {
47
48
        $ids            = $request->get('ids');
49
        $local_override = $this->getParams($request, 'local_override', null);
50
51
        TranslationUtils::overrideLocal($local_override);
52
53
        if (empty($query)) {
54
            $query = $this->model;
55
        }
56
57
        if (!empty($ids)) {
58
            return response()->json($this->generateResultSearchByIds($request, $query, $ids));
59
        }
60
61
        return response()->json($this->generateResultSearch($request, $query));
62
63
    }
64
65
    protected function generateResultSearchByIds(Request $request, $query, $ids)
66
    {
67
        $no_edit = $request->get('no_edit');
68
69
        if (!empty($no_edit) && method_exists($this->model, 'withoutTranslation')) {
70
            $query = $query->withoutTranslation();
71
        }
72
73
        $data = $query->whereIn($this->model->getKeyName(), $ids)->get();
74
75
        return $data;
76
    }
77
78
    protected function generateResultSearch(Request $request, $query)
79
    {
80
        $term  = $request->get('term');
81
        $page  = $this->getParams($request, 'page', 1);
82
        $paged = $this->getParams($request, 'page_limit', 10);
83
84
        if (empty($term)) {
85
            $elements = array();
86
            $total    = 0;
87
        } else {
88
            $elements = $query->search($term)->take($paged)->skip(($page - 1) * $paged)->get();
89
            $total    = $query->search($term)->count();
90
91
        }
92
93
        return [
94
            'total'    => $total,
95
            'elements' => $elements
96
        ];
97
    }
98
99
    protected function getParams(Request $request, $key, $default_value)
100
    {
101
        $element = $request->get($key);
102
103
        if (empty($element)) {
104
            $element = $default_value;
105
        }
106
107
        return $element;
108
    }
109
110
}