Completed
Push — master ( 8927df...f70445 )
by Abdelrahman
02:05
created

UsersController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 10
Bugs 1 Features 0
Metric Value
dl 0
loc 217
rs 10
c 10
b 1
f 0
wmc 20
lcom 1
cbo 3

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A index() 0 6 1
A show() 0 11 3
A create() 0 4 1
A copy() 0 4 1
A edit() 0 4 1
A store() 0 4 1
A update() 0 4 1
A delete() 0 9 1
A import() 0 4 1
A export() 0 4 1
A bulk() 0 4 1
A form() 0 14 1
B process() 0 23 5
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Http\Controllers\Backend;
17
18
use Illuminate\Http\Request;
19
use Rinvex\Fort\Models\User;
20
use Rinvex\Fort\Contracts\UserRepositoryContract;
21
use Rinvex\Fort\Http\Controllers\AuthorizedController;
22
use Rinvex\Fort\Http\Requests\Backend\UserStoreRequest;
23
use Rinvex\Fort\Http\Requests\Backend\UserUpdateRequest;
24
25
class UsersController extends AuthorizedController
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected $resource = 'users';
31
32
    /**
33
     * The user repository instance.
34
     *
35
     * @var \Rinvex\Fort\Contracts\UserRepositoryContract
36
     */
37
    protected $userRepository;
38
39
    /**
40
     * Create a new users controller instance.
41
     */
42
    public function __construct(UserRepositoryContract $userRepository)
43
    {
44
        parent::__construct();
45
46
        $this->userRepository = $userRepository;
47
    }
48
49
    /**
50
     * Display a listing of the resource.
51
     *
52
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
53
     */
54
    public function index()
55
    {
56
        $users = $this->userRepository->paginate(config('rinvex.fort.backend.items_per_page'));
57
58
        return view('rinvex/fort::backend/users.index', compact('users'));
59
    }
60
61
    /**
62
     * Display the specified resource.
63
     *
64
     * @param \Rinvex\Fort\Models\User $user
65
     *
66
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
67
     */
68
    public function show(User $user)
69
    {
70
        $resources   = app('rinvex.fort.ability')->findAll()->groupBy('resource');
71
        $actions     = ['list', 'view', 'create', 'update', 'delete', 'import', 'export'];
72
        $columns     = ['resource', 'list', 'view', 'create', 'update', 'delete', 'import', 'export', 'other'];
73
        $userCountry = country($user->country);
74
        $country     = ! empty($userCountry) ? $userCountry->getName().' '.$userCountry->getEmoji() : null;
75
        $phone       = ! empty($userCountry) ? $userCountry->getCallingCode().$user->phone : null;
76
77
        return view('rinvex/fort::backend/users.show', compact('user', 'resources', 'actions', 'columns', 'country', 'phone'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 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...
78
    }
79
80
    /**
81
     * Show the form for creating a new resource.
82
     *
83
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
84
     */
85
    public function create()
86
    {
87
        return $this->form('create', 'store', $this->userRepository->createModel());
88
    }
89
90
    /**
91
     * Show the form for copying the given resource.
92
     *
93
     * @param \Rinvex\Fort\Models\User $user
94
     *
95
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
96
     */
97
    public function copy(User $user)
98
    {
99
        return $this->form('copy', 'store', $user);
100
    }
101
102
    /**
103
     * Show the form for editing the given resource.
104
     *
105
     * @param \Rinvex\Fort\Models\User $user
106
     *
107
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
108
     */
109
    public function edit(User $user)
110
    {
111
        return $this->form('edit', 'update', $user);
112
    }
113
114
    /**
115
     * Store a newly created resource in storage.
116
     *
117
     * @param \Rinvex\Fort\Http\Requests\Backend\UserStoreRequest $request
118
     *
119
     * @return \Illuminate\Http\Response
120
     */
121
    public function store(UserStoreRequest $request)
122
    {
123
        return $this->process($request);
124
    }
125
126
    /**
127
     * Update the given resource in storage.
128
     *
129
     * @param \Rinvex\Fort\Http\Requests\Backend\UserUpdateRequest $request
130
     * @param \Rinvex\Fort\Models\User                             $user
131
     *
132
     * @return \Illuminate\Http\Response
133
     */
134
    public function update(UserUpdateRequest $request, User $user)
135
    {
136
        return $this->process($request, $user);
137
    }
138
139
    /**
140
     * Delete the given resource from storage.
141
     *
142
     * @param \Rinvex\Fort\Models\User $user
143
     *
144
     * @return \Illuminate\Http\Response
145
     */
146
    public function delete(User $user)
147
    {
148
        $result = $this->userRepository->delete($user);
149
150
        return intend([
151
            'route' => 'rinvex.fort.backend.users.index',
152
            'with'  => ['rinvex.fort.alert.warning' => trans('rinvex/fort::backend/messages.user.deleted', ['userId' => $result->id])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
153
        ]);
154
    }
155
156
    /**
157
     * Import the given resources into storage.
158
     *
159
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
160
     */
161
    public function import()
162
    {
163
        //
164
    }
165
166
    /**
167
     * Export the given resources from storage.
168
     *
169
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
170
     */
171
    public function export()
172
    {
173
        //
174
    }
175
176
    /**
177
     * Bulk control the given resources.
178
     *
179
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
180
     */
181
    public function bulk()
182
    {
183
        //
184
    }
185
186
    /**
187
     * Show the form for create/edit/copy of the given resource.
188
     *
189
     * @param string                   $mode
190
     * @param string                   $action
191
     * @param \Rinvex\Fort\Models\User $user
192
     *
193
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
194
     */
195
    protected function form($mode, $action, User $user)
196
    {
197
        $countries = array_map(function ($country) {
198
            return $country->getName();
199
        }, Loader::countries());
200
201
        $abilityList = app('rinvex.fort.ability')->findAll()->groupBy('resource')->map(function ($item) {
202
            return $item->pluck('title', 'id');
203
        })->toArray();
204
205
        $roleList = app('rinvex.fort.role')->findAll()->pluck('title', 'id')->toArray();
206
207
        return view('rinvex/fort::backend/users.form', compact('user', 'abilityList', 'roleList', 'countries', 'mode', 'action'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
208
    }
209
210
    /**
211
     * Process the form for store/update of the given resource.
212
     *
213
     * @param \Illuminate\Http\Request $request
214
     * @param \Rinvex\Fort\Models\User $user
0 ignored issues
show
Documentation introduced by
Should the type for parameter $user not be null|User?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
215
     *
216
     * @return \Illuminate\Http\Response
217
     */
218
    protected function process(Request $request, User $user = null)
219
    {
220
        // Prepare required input fields
221
        $input     = $request->except(['_method', '_token', 'id']);
222
        $roles     = $request->user($this->getGuard())->can('assign-roles') ? ['roles' => array_pull($input, 'roleList')] : [];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 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...
223
        $abilities = $request->user($this->getGuard())->can('grant-abilities') ? ['abilities' => array_pull($input, 'abilityList')] : [];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 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...
224
225
        // Store data into the entity
226
        $result = $this->userRepository->store($user, $input + $roles + $abilities);
227
228
        // Repository `store` method returns false if no attributes
229
        // updated, happens save button clicked without chaning anything
230
        $message = ! is_null($user)
231
            ? ($result === false
232
                ? ['rinvex.fort.alert.warning' => trans('rinvex/fort::backend/messages.user.nothing_updated', ['userId' => $user->id])]
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
233
                : ['rinvex.fort.alert.success' => trans('rinvex/fort::backend/messages.user.updated', ['userId' => $result->id])])
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
234
            : ['rinvex.fort.alert.success' => trans('rinvex/fort::backend/messages.user.created', ['userId' => $result->id])];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 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...
235
236
        return intend([
237
            'route' => 'rinvex.fort.backend.users.index',
238
            'with'  => $message,
239
        ]);
240
    }
241
}
242