Passed
Push — master ( fa5159...8fd79e )
by Darko
11:05
created

AdminCategoryRegexesController::edit()   B

Complexity

Conditions 10
Paths 2

Size

Total Lines 81
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
c 1
b 0
f 0
dl 0
loc 81
rs 7.0496
cc 10
nc 2
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\BasePageController;
6
use App\Models\Category;
7
use Blacklight\Regexes;
8
use Illuminate\Http\Request;
9
10
class AdminCategoryRegexesController extends BasePageController
11
{
12
    /**
13
     * @throws \Exception
14
     */
15
    public function index(Request $request): void
16
    {
17
        $this->setAdminPrefs();
18
        $regexes = new Regexes(['Settings' => null, 'Table_Name' => 'category_regexes']);
19
20
        $meta_title = $title = 'Category Regex List';
21
22
        $group = $request->has('group') && ! empty($request->input('group')) ? $request->input('group') : '';
23
        $regex = $regexes->getRegex($group);
24
25
        $this->smarty->assign(
26
            [
27
                'group' => $group,
28
                'regex' => $regex,
29
            ]
30
        );
31
32
        $content = $this->smarty->fetch('category_regexes-list.tpl');
33
34
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
35
36
        $this->adminrender();
37
    }
38
39
    /**
40
     * @return \Illuminate\Http\RedirectResponse|void
41
     *
42
     * @throws \Exception
43
     */
44
    public function edit(Request $request)
45
    {
46
        $this->setAdminPrefs();
47
        $regexes = new Regexes(['Settings' => null, 'Table_Name' => 'category_regexes']);
48
49
        // Set the current action.
50
        $action = $request->input('action') ?? 'view';
51
52
        $regex = [
53
            'id' => '',
54
            'group_regex' => '',
55
            'regex' => '',
56
            'description' => '',
57
            'ordinal' => '',
58
            'categories_id' => '',
59
            'status' => 1, ];
60
61
        $this->smarty->assign('regex', $regex);
62
63
        switch ($action) {
64
            case 'submit':
65
                if (empty($request->input('group_regex'))) {
66
                    $this->smarty->assign('error', 'Group regex must not be empty!');
67
                    break;
68
                }
69
70
                if (empty($request->input('regex'))) {
71
                    $this->smarty->assign('error', 'Regex cannot be empty');
72
                    break;
73
                }
74
75
                if (! is_numeric($request->input('ordinal')) || $request->input('ordinal') < 0) {
76
                    $this->smarty->assign('error', 'Ordinal must be a number, 0 or higher.');
77
                    break;
78
                }
79
80
                if (empty($request->input('id'))) {
81
                    $regexes->addRegex($request->all());
82
                } else {
83
                    $regexes->updateRegex($request->all());
84
                }
85
86
                return redirect()->to('admin/category_regexes-list');
87
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
88
89
            case 'view':
90
            default:
91
                if ($request->has('id')) {
92
                    $meta_title = $title = 'Category Regex Edit';
93
                    $id = $request->input('id');
94
                    $regex = $regexes->getRegexByID($id);
95
                } else {
96
                    $meta_title = $title = 'Category Regex Add';
97
                }
98
                $this->smarty->assign('regex', $regex);
99
                break;
100
        }
101
102
        $this->smarty->assign('status_ids', [Category::STATUS_ACTIVE, Category::STATUS_INACTIVE]);
103
        $this->smarty->assign('status_names', ['Yes', 'No']);
104
105
        $categories_db = Category::query()
106
            ->select(['c.id', 'c.title', 'cp.title as parent_title'])
107
            ->from('categories as c')
0 ignored issues
show
Bug introduced by
'categories as c' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $table of Illuminate\Database\Query\Builder::from(). ( Ignorable by Annotation )

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

107
            ->from(/** @scrutinizer ignore-type */ 'categories as c')
Loading history...
108
            ->join('root_categories as cp', 'c.root_categories_id', '=', 'cp.id')
109
            ->whereNotNull('c.root_categories_id')
110
            ->orderBy('c.id')
0 ignored issues
show
Bug introduced by
'c.id' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderBy(). ( Ignorable by Annotation )

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

110
            ->orderBy(/** @scrutinizer ignore-type */ 'c.id')
Loading history...
111
            ->get();
112
        // Build arrays for Smarty html_options helper. Previously only last row was kept.
113
        $category_ids = [];
114
        $category_names = [];
115
        foreach ($categories_db as $category_db) {
116
            $category_ids[] = $category_db->id;
117
            $category_names[] = $category_db->parent_title.' '.$category_db->title.': '.$category_db->id;
118
        }
119
        $this->smarty->assign('category_names', $category_names);
120
        $this->smarty->assign('category_ids', $category_ids);
121
122
        $content = $this->smarty->fetch('category_regexes-edit.tpl');
123
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
124
        $this->adminrender();
125
    }
126
}
127