Completed
Push — settings-management ( 58b7ea )
by Fèvre
06:34 queued 06:34
created

SettingController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\View\View;
7
use Xetaravel\Http\Controllers\Admin\Controller;
8
use Xetaravel\Models\Setting;
9
use Xetaravel\Models\Repositories\SettingRepository;
10
use Xetaravel\Models\Validators\SettingValidator;
11
12
class SettingController extends Controller
13
{
14
    /**
15
     * Show all the settings.
16
     *
17
     * @return \Illuminate\View\View
18
     */
19
    public function index(): View
20
    {
21
        $settings = Setting::paginate(10);
22
23
        $breadcrumbs = $this->breadcrumbs->addCrumb('Manage Settings', route('admin.setting.index'));
24
25
        return view('Admin::Setting.index', compact('settings', 'breadcrumbs'));
26
    }
27
28
    /**
29
     * Show the setting create form.
30
     *
31
     * @return \Illuminate\View\View
32
     */
33
    public function showCreateForm(): View
34
    {
35
        $breadcrumbs = $this->breadcrumbs
36
            ->addCrumb('Manage Settings', route('admin.setting.index'))
37
            ->addCrumb("Create", route('admin.setting.create'));
38
39
        return view('Admin::Setting.create', compact('breadcrumbs'));
40
    }
41
42
    /**
43
     * Handle a setting create request for the application.
44
     *
45
     * @param \Illuminate\Http\Request $request
46
     *
47
     * @return \Illuminate\Http\RedirectResponse
48
     */
49
    public function create(Request $request): RedirectResponse
50
    {
51
        SettingValidator::create($request->all())->validate();
52
        SettingRepository::create($request->all());
53
54
        return redirect()
55
            ->route('admin.setting.index')
56
            ->with('success', 'This setting has been created successfully !');
57
    }
58
59
    /**
60
     * Show the update form.
61
     *
62
     * @param \Illuminate\Http\Request $request
63
     * @param int $id The id of the setting.
64
     *
65
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
66
     */
67
    public function showUpdateForm(Request $request, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

67
    public function showUpdateForm(/** @scrutinizer ignore-unused */ Request $request, int $id)

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

Loading history...
68
    {
69
        $setting = Setting::findOrFail($id);
70
71
        $breadcrumbs = $this->breadcrumbs
72
            ->setListElementClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0')
73
            ->addCrumb('Manage Settings', route('admin.setting.index'))
74
            ->addCrumb(
75
                'Update ' . e($setting->name),
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Xetaravel\Models\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
76
                route('admin.setting.update', $setting->name, $setting->id)
0 ignored issues
show
Bug introduced by
$setting->id of type integer is incompatible with the type boolean expected by parameter $absolute of route(). ( Ignorable by Annotation )

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

76
                route('admin.setting.update', $setting->name, /** @scrutinizer ignore-type */ $setting->id)
Loading history...
77
            );
78
79
        return view(
80
            'Admin::Setting.update',
81
            compact('setting', 'breadcrumbs')
82
        );
83
    }
84
85
    /**
86
     * Handle an setting update request for the application.
87
     *
88
     * @param \Illuminate\Http\Request $request
89
     * @param int $id The id of the setting to update.
90
     *
91
     * @return \Illuminate\Http\RedirectResponse
92
     */
93
    public function update(Request $request, int $id): RedirectResponse
94
    {
95
        $setting = Setting::findOrFail($id);
96
97
        SettingValidator::update($request->all(), $setting->id)->validate();
98
        SettingRepository::update($request->all(), $setting);
99
100
        return redirect()
101
            ->route('admin.setting.index')
102
            ->with('success', 'This setting has been updated successfully !');
103
    }
104
105
    /**
106
     * Handle the delete request for the setting.
107
     *
108
     * @param int $id The id of the setting to delete.
109
     *
110
     * @return \Illuminate\Http\RedirectResponse
111
     */
112
    public function delete(int $id): RedirectResponse
113
    {
114
        $setting = Setting::findOrFail($id);
115
116
        if (!$setting->is_deletable) {
0 ignored issues
show
Bug Best Practice introduced by
The property is_deletable does not exist on Xetaravel\Models\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
117
            return redirect()
118
                ->route('admin.setting.index')
119
                ->with('danger', 'You can not delete this setting !');
120
        }
121
122
        if ($setting->delete()) {
123
            return redirect()
124
                ->route('admin.setting.index')
125
                ->with('success', 'This setting has been deleted successfully !');
126
        }
127
128
        return redirect()
129
            ->route('admin.setting.index')
130
            ->with('danger', 'An error occurred while deleting this setting !');
131
    }
132
}
133