We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 13 |
Paths | 1 |
Total Lines | 67 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 12 | ||
Bugs | 5 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
18 | public function updateTreeOrder($request) |
||
19 | { |
||
20 | $primaryKey = $this->model->getKeyName(); |
||
21 | |||
22 | $columns = $this->getOperationSetting('reorderColumnNames'); |
||
|
|||
23 | |||
24 | // we use the upsert method that should update the values of the matching ids. |
||
25 | // it has the drawback of creating new entries when the id is not found |
||
26 | // for that reason we get a list of all the ids and filter the ones |
||
27 | // sent in the request that are not in the database |
||
28 | $itemKeys = $this->model->query()->select($primaryKey)->get()->pluck($primaryKey); |
||
29 | |||
30 | // filter the items that are not in the database and map the request |
||
31 | $reorderItems = collect($request)->filter(function ($item) use ($itemKeys) { |
||
32 | return $item['item_id'] !== '' && $item['item_id'] !== null && $itemKeys->contains($item['item_id']); |
||
33 | })->map(function ($item) use ($primaryKey, $columns) { |
||
34 | $item[$primaryKey] = $item['item_id']; |
||
35 | $item[$columns['parent_id']] = empty($item['parent_id']) ? null : $item['parent_id']; |
||
36 | $item[$columns['depth']] = empty($item['depth']) ? null : (int) $item['depth']; |
||
37 | $item[$columns['lft']] = empty($item['left']) ? null : (int) $item['left']; |
||
38 | $item[$columns['rgt']] = empty($item['right']) ? null : (int) $item['right']; |
||
39 | |||
40 | // unset mapped items properties. |
||
41 | if($columns['parent_id'] !== 'parent_id') { unset($item['parent_id']); }; |
||
42 | if($columns['depth'] !== 'depth') { unset($item['depth']); } |
||
43 | if($columns['lft'] !== 'left') { unset($item['left']); } |
||
44 | if($columns['rgt'] !== 'right') { unset($item['right']); } |
||
45 | |||
46 | // unset the item_id property |
||
47 | unset($item['item_id']); |
||
48 | |||
49 | return $item; |
||
50 | })->toArray(); |
||
51 | |||
52 | $sentIds = array_column($reorderItems, $primaryKey); |
||
53 | |||
54 | $itemKeys = $itemKeys->filter(function ($id) use ($sentIds) { |
||
55 | return in_array($id, $sentIds); |
||
56 | }); |
||
57 | |||
58 | // wrap the queries in a transaction to avoid partial updates |
||
59 | DB::connection($this->model->getConnectionName())->transaction(function () use ($reorderItems, $primaryKey, $itemKeys, $columns) { |
||
60 | // create a string of ?,?,?,? to use as bind placeholders for item keys |
||
61 | $reorderItemsBindString = implode(',', array_fill(0, count($reorderItems), '?')); |
||
62 | |||
63 | // each of this properties will be updated using a single query with a CASE statement |
||
64 | // this ensures that only 4 queries are run, no matter how many items are reordered |
||
65 | foreach (array_values($columns) as $column) { |
||
66 | $query = ''; |
||
67 | $bindings = []; |
||
68 | $query .= "UPDATE {$this->model->getTable()} SET {$column} = CASE "; |
||
69 | foreach ($reorderItems as $item) { |
||
70 | $query .= "WHEN {$primaryKey} = ? THEN ? "; |
||
71 | $bindings[] = $item[$primaryKey]; |
||
72 | $bindings[] = $item[$column]; |
||
73 | } |
||
74 | // add the bind placeholders for the item keys at the end the array of bindings |
||
75 | array_push($bindings, ...$itemKeys->toArray()); |
||
76 | |||
77 | // add the where clause to the query to help match the items |
||
78 | $query .= "ELSE {$column} END WHERE {$primaryKey} IN ({$reorderItemsBindString})"; |
||
79 | |||
80 | DB::connection($this->model->getConnectionName())->statement($query, $bindings); |
||
81 | } |
||
82 | }); |
||
83 | |||
84 | return count($reorderItems); |
||
85 | } |
||
119 |