Completed
Push — develop ( c763e2...508664 )
by Abdelrahman
16:47
created

GuardiansController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Illuminate\Http\Request;
8
use Cortex\Auth\Models\Guardian;
9
use Illuminate\Foundation\Http\FormRequest;
10
use Cortex\Foundation\DataTables\LogsDataTable;
11
use Cortex\Auth\DataTables\Adminarea\GuardiansDataTable;
12
use Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest;
13
use Cortex\Foundation\Http\Controllers\AuthorizedController;
14
15
class GuardiansController extends AuthorizedController
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected $resource = 'guardian';
21
22
    /**
23
     * List all guardians.
24
     *
25
     * @param \Cortex\Auth\DataTables\Adminarea\GuardiansDataTable $guardiansDataTable
26
     *
27
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
28
     */
29
    public function index(GuardiansDataTable $guardiansDataTable)
30
    {
31
        return $guardiansDataTable->with([
32
            'id' => 'adminarea-guardians-index-table',
33
            'phrase' => trans('cortex/auth::common.guardians'),
34
        ])->render('cortex/foundation::adminarea.pages.datatable');
35
    }
36
37
    /**
38
     * List guardian logs.
39
     *
40
     * @param \Cortex\Auth\Models\Guardian                $guardian
41
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
42
     *
43
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...e|\Illuminate\View\View?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45
    public function logs(Guardian $guardian, LogsDataTable $logsDataTable)
46
    {
47
        return $logsDataTable->with([
48
            'resource' => $guardian,
49
            'tabs' => 'adminarea.guardians.tabs',
50
            'phrase' => trans('cortex/auth::common.guardians'),
51
            'id' => "adminarea-guardians-{$guardian->getKey()}-logs-table",
52
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
53
    }
54
55
    /**
56
     * Create new guardian.
57
     *
58
     * @param \Illuminate\Http\Request     $request
59
     * @param \Cortex\Auth\Models\Guardian $guardian
60
     *
61
     * @return \Illuminate\View\View
62
     */
63
    public function create(Request $request, Guardian $guardian)
64
    {
65
        return $this->form($request, $guardian);
66
    }
67
68
    /**
69
     * Edit given guardian.
70
     *
71
     * @param \Illuminate\Http\Request     $request
72
     * @param \Cortex\Auth\Models\Guardian $guardian
73
     *
74
     * @return \Illuminate\View\View
75
     */
76
    public function edit(Request $request, Guardian $guardian)
77
    {
78
        return $this->form($request, $guardian);
79
    }
80
81
    /**
82
     * Show guardian create/edit form.
83
     *
84
     * @param \Illuminate\Http\Request     $request
85
     * @param \Cortex\Auth\Models\Guardian $guardian
86
     *
87
     * @return \Illuminate\View\View
88
     */
89
    protected function form(Request $request, Guardian $guardian)
90
    {
91
        $countries = collect(countries())->map(function ($country, $code) {
92
            return [
93
                'id' => $code,
94
                'text' => $country['name'],
95
                'emoji' => $country['emoji'],
96
            ];
97
        })->values();
98
        $currentUser = $request->user($this->getGuard());
99
        $languages = collect(languages())->pluck('name', 'iso_639_1');
100
        $genders = ['male' => trans('cortex/auth::common.male'), 'female' => trans('cortex/auth::common.female')];
101
102
        $roles = $currentUser->can('superadmin')
103
            ? app('cortex.auth.role')->all()->pluck('name', 'id')->toArray()
104
            : $currentUser->roles->pluck('name', 'id')->toArray();
105
106
        $abilities = $currentUser->can('superadmin')
107
            ? app('cortex.auth.ability')->all()->pluck('title', 'id')->toArray()
108
            : $currentUser->abilities->pluck('title', 'id')->toArray();
109
110
        return view('cortex/auth::adminarea.pages.guardian', compact('guardian', 'abilities', 'roles', 'countries', 'languages', 'genders'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 141 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
111
    }
112
113
    /**
114
     * Store new guardian.
115
     *
116
     * @param \Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest $request
117
     * @param \Cortex\Auth\Models\Guardian                             $guardian
118
     *
119
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
120
     */
121
    public function store(GuardianFormRequest $request, Guardian $guardian)
122
    {
123
        return $this->process($request, $guardian);
124
    }
125
126
    /**
127
     * Update given guardian.
128
     *
129
     * @param \Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest $request
130
     * @param \Cortex\Auth\Models\Guardian                             $guardian
131
     *
132
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
133
     */
134
    public function update(GuardianFormRequest $request, Guardian $guardian)
135
    {
136
        return $this->process($request, $guardian);
137
    }
138
139
    /**
140
     * Process stored/updated guardian.
141
     *
142
     * @param \Illuminate\Foundation\Http\FormRequest $request
143
     * @param \Cortex\Auth\Models\Guardian            $guardian
144
     *
145
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
146
     */
147
    protected function process(FormRequest $request, Guardian $guardian)
148
    {
149
        // Prepare required input fields
150
        $data = $request->validated();
151
152
        // Save guardian
153
        $guardian->fill($data)->save();
154
155
        return intend([
156
            'url' => route('adminarea.guardians.index'),
157
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'guardian', 'id' => $guardian->username])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 146 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
158
        ]);
159
    }
160
161
    /**
162
     * Destroy given guardian.
163
     *
164
     * @param \Cortex\Auth\Models\Guardian $guardian
165
     *
166
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
167
     */
168
    public function destroy(Guardian $guardian)
169
    {
170
        $guardian->delete();
171
172
        return intend([
173
            'url' => route('adminarea.guardians.index'),
174
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'guardian', 'id' => $guardian->username])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 148 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
175
        ]);
176
    }
177
}
178