Completed
Push — master ( f65d8f...4f3de5 )
by Pavel
04:14 queued 01:54
created

CrudController::actionDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace App\Controller;
3
4
use App\Common\Helper;
5
use App\Common\JsonException;
6
use App\Scopes\MaxPerPageScope;
7
8
use Slim\Http\Request;
9
use Slim\Http\Response;
10
11
class CrudController extends BaseController
12
{
13
    /**
14
     * @param Request  $request
15
     * @param Response $response
16
     * @param array    $args
17
     *
18
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Psr\Http\Message\ResponseInterface.

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...
19
     */
20
    public function actionIndex(Request $request, Response $response, $args)
21
    {
22
        $modelName = 'App\Model\\'.Helper::dashesToCamelCase($args['entity'], true);
23
        $params    = $request->getQueryParams();
24
        $query     = $modelName::CurrentUser();
25
26
        if (isset($params['withTrashed']) && $params['withTrashed'] == 1) {
27
            $query = $modelName::withTrashed();
28
        }
29
30
        if (isset($params['filters'])) {
31
            $filters = json_decode($params['filters'], true);
32
33
            foreach ($filters as $filter) {
34
                $filter['operator']  = trim(strtolower($filter['operator']));
35
                $filter['attribute'] = trim($filter['attribute']);
36
37
                if (empty($filter['operator']) || empty($filter['attribute']) || empty($filter['value'])) continue;
38
39
                switch ($filter['operator']) {
40
                    case 'in':
41
                        $query = $query->whereIn($filter['attribute'], $filter['value']);
42
                        break;
43
                    case 'not in':
44
                        $query = $query->whereNotIn($filter['attribute'], $filter['value']);
45
                        break;
46
                    case 'like':
47
                        $query = $query->where($filter['attribute'], 'like', '%' . $filter['value'] . '%');
48
                        break;
49
                    case '=':
50
                    case '!=':
51
                    case '>':
52
                    case '>=':
53
                    case '<':
54
                    case '<=':
55
                        $query = $query->where($filter['attribute'], $filter['operator'], $filter['value']);
56
                        break;
57
                }
58
            }
59
        }
60
61
        if (isset($params['sort'])) {
62
            $sorters = json_decode($params['sort'], true);
63
64
            foreach ($sorters as $sorter) {
65
                $sorter['direction'] = trim(strtolower($sorter['direction'])) == 'asc' ? 'asc' : 'desc';
66
                $query->orderBy(trim($sorter['attribute']), $sorter['direction']);
67
            }
68
        }
69
70
        $pageNumber = null;
71
        $pageSize   = null;
72
        if (isset($params['page']['number'])) {
73
            $pageNumber = $params['page']['number'];
74
            $pageSize   = (isset($params['page']['size']) && $params['page']['size'] <= 100) ? $params['page']['size'] : 15;
75
            $entities   = $query->withoutGlobalScopes([MaxPerPageScope::class])->paginate($pageSize, ['*'], 'page', $pageNumber);
76
        } else {
77
            $entities = $query->get();
78
        }
79
80
        $result = $this->encode($request, $entities, $pageNumber, $pageSize);
81
82
        return $this->renderer->jsonApiRender($response, 200, $result);
83
    }
84
85
    /**
86
     * @param Request  $request
87
     * @param Response $response
88
     * @param array    $args
89
     *
90
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Psr\Http\Message\ResponseInterface.

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...
91
     * @throws JsonException
92
     */
93
    public function actionGet(Request $request, Response $response, $args)
94
    {
95
        $modelName = 'App\Model\\'.Helper::dashesToCamelCase($args['entity'], true);
96
        $query     = $modelName::CurrentUser();
97
        $entity    = $query->find($args['id']);
98
99
        if (!$entity) {
100
            throw new JsonException($args['entity'], 404, 'Not found', 'Entity not found');
101
        }
102
103
        $result = $this->encode($request, $entity);
104
105
        return $this->renderer->jsonApiRender($response, 200, $result);
106
    }
107
108
    /**
109
     * @param Request  $request
110
     * @param Response $response
111
     * @param array    $args
112
     *
113
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Psr\Http\Message\ResponseInterface.

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...
114
     * @throws JsonException
115
     */
116
    public function actionCreate(Request $request, Response $response, $args)
117
    {
118
        $modelName    = 'App\Model\\'.Helper::dashesToCamelCase($args['entity'], true);
119
        $requestClass = 'App\Requests\\'.Helper::dashesToCamelCase($args['entity'], true).'CreateRequest';
120
        $params       = $request->getParsedBody();
121
122
        $this->validationRequest($params, $args['entity'], new $requestClass());
123
124
        $entity = $modelName::create($params['data']['attributes']);
125
        $result = $this->encode($request, $entity);
126
127
        return $this->renderer->jsonApiRender($response, 200, $result);
128
129
    }
130
131
    /**
132
     * @param Request  $request
133
     * @param Response $response
134
     * @param array    $args
135
     *
136
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Psr\Http\Message\ResponseInterface.

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...
137
     * @throws JsonException
138
     */
139
    public function actionUpdate(Request $request, Response $response, $args)
140
    {
141
        $modelName    = 'App\Model\\'.Helper::dashesToCamelCase($args['entity'], true);
142
        $requestClass = 'App\Requests\\'.Helper::dashesToCamelCase($args['entity'], true).'UpdateRequest';
143
        $params       = $request->getParsedBody();
144
        $query        = $modelName::CurrentUser();
145
        $entity       = $query->find($args['id']);
146
147
        if (!$entity) {
148
            throw new JsonException($args['entity'], 404, 'Not found', 'Entity not found');
149
        }
150
151
        $this->validationRequest($params, $args['entity'], new $requestClass());
152
153
        $entity->update($params['data']['attributes']);
154
155
        $result = $this->encode($request, $entity);
156
157
        return $this->renderer->jsonApiRender($response, 200, $result);
158
    }
159
160
    /**
161
     * @param Request  $request
162
     * @param Response $response
163
     * @param array    $args
164
     *
165
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Psr\Http\Message\ResponseInterface.

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...
166
     * @throws JsonException
167
     */
168
    public function actionDelete(Request $request, Response $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
169
    {
170
        $modelName = 'App\Model\\'.Helper::dashesToCamelCase($args['entity'], true);
171
        $query     = $modelName::CurrentUser();
172
        $entity    = $query->find($args['id']);
173
174
        if (!$entity) {
175
            throw new JsonException($args['entity'], 404, 'Not found', 'Entity not found');
176
        }
177
178
        $entity->delete();
179
180
        return $this->renderer->jsonApiRender($response, 204);
181
    }
182
183
}