Completed
Push — master ( 2c722f...4f3f82 )
by Maxime
20:28
created

ModelBaseController::generateResultSearch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.0017

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 12
cts 13
cp 0.9231
rs 9.4285
cc 2
eloc 13
nc 2
nop 2
crap 2.0017
1
<?php namespace Distilleries\Expendable\Http\Controllers\Admin\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 240
    public function __construct(BaseModel $model, LayoutManagerContract $layoutManager)
20
    {
21 240
        parent::__construct($layoutManager);
22 240
        $this->model = $model;
23 180
    }
24
25
    // ------------------------------------------------------------------------------------------------
26
    // ------------------------------------------------------------------------------------------------
27
    // ------------------------------------------------------------------------------------------------
28
29 48
    public function putDestroy(Request $request)
30
    {
31 48
        $validation = \Validator::make($request->all(), [
32
            'id' => 'required'
33 48
        ]);
34 48
        if ($validation->fails()) {
35 24
            return redirect()->back()->withErrors($validation)->withInput($request->all());
36
        }
37
38 24
        $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 24
        $data->delete();
40
41 24
        return redirect()->to(action('\\' . get_class($this) . '@getIndex'));
42
    }
43
44
    // ------------------------------------------------------------------------------------------------
45 12
    public function postSearch(Request $request, $query = null)
46
    {
47
48 12
        $ids            = $request->get('ids');
49 12
        $local_override = $this->getParams($request, 'local_override', null);
50
51 12
        TranslationUtils::overrideLocal($local_override);
52
53 12
        if (empty($query)) {
54 8
            $query = $this->model;
55 6
        }
56
57 12
        if (!empty($ids)) {
58 12
            return response()->json($this->generateResultSearchByIds($request, $query, $ids));
59
        }
60
61 8
        return response()->json($this->generateResultSearch($request, $query));
62
63
    }
64
65 12
    protected function generateResultSearchByIds(Request $request, $query, $ids)
66
    {
67 12
        $no_edit = $request->get('no_edit');
68
69 12
        if (!empty($no_edit) && method_exists($this->model, 'withoutTranslation')) {
70
            $query = $query->withoutTranslation();
71
        }
72
73 12
        $data = $query->whereIn($this->model->getKeyName(), $ids)->get();
74
75 12
        return $data;
76
    }
77
78 8
    protected function generateResultSearch(Request $request, $query)
79
    {
80 8
        $term  = $request->get('term');
81 8
        $page  = $this->getParams($request, 'page', 1);
82 8
        $paged = $this->getParams($request, 'page_limit', 10);
83
84 8
        if (empty($term)) {
85 4
            $elements = array();
86 4
            $total    = 0;
87 3
        } else {
88 4
            $elements = $query->search($term)->take($paged)->skip(($page - 1) * $paged)->get();
89
            $total    = $query->search($term)->count();
90
91
        }
92
93
        return [
94 4
            'total'    => $total,
95 1
            'elements' => $elements
96 3
        ];
97
    }
98
99 12
    protected function getParams(Request $request, $key, $default_value)
100
    {
101 12
        $element = $request->get($key);
102
103 12
        if (empty($element)) {
104 12
            $element = $default_value;
105 9
        }
106
107 12
        return $element;
108
    }
109
110
}