Completed
Push — master ( 57bab5...72813b )
by Abdelrahman
02:34
created

UsersController::form()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
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 Rinvex\Country\Loader;
19
use Illuminate\Http\Request;
20
use Rinvex\Fort\Models\User;
21
use Rinvex\Fort\Contracts\UserRepositoryContract;
22
use Rinvex\Fort\Http\Controllers\AuthorizedController;
23
24
class UsersController extends AuthorizedController
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected $resourceAbilityMap = [
30
        'activate'   => 'activate',
31
        'deactivate' => 'deactivate',
32
    ];
33
34
    /**
35
     * The user repository instance.
36
     *
37
     * @var \Rinvex\Fort\Contracts\UserRepositoryContract
38
     */
39
    protected $userRepository;
40
41
    /**
42
     * Create a new users controller instance.
43
     *
44
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
45
     */
46
    public function __construct(UserRepositoryContract $userRepository)
47
    {
48
        parent::__construct();
49
50
        $this->authorizeResource(User::class);
51
52
        $this->userRepository = $userRepository;
53
    }
54
55
    /**
56
     * Display a listing of the resource.
57
     *
58
     * @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...
59
     */
60
    public function index()
61
    {
62
        $users = $this->userRepository->paginate(config('rinvex.fort.backend.items_per_page'));
63
64
        return view('rinvex.fort::backend.users.index', compact('users'));
65
    }
66
67
    /**
68
     * Display the specified resource.
69
     *
70
     * @param int $id
71
     *
72
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...\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...
73
     */
74
    public function show($id)
75
    {
76
        if (! $user = $this->userRepository->find($id)) {
77
            return intend([
78
                'intended'   => route('rinvex.fort.backend.users.index'),
79
                'withErrors' => ['rinvex.fort.user.not_found' => trans('rinvex.fort::backend/messages.user.not_found', ['user' => $id])],
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...
80
            ]);
81
        }
82
83
        $actions     = ['view', 'create', 'edit', 'delete', 'import', 'export'];
84
        $resources   = app('rinvex.fort.ability')->findAll()->groupBy('resource');
85
        $columns     = ['resource', 'view', 'create', 'edit', 'delete', 'import', 'export', 'other'];
86
        $userCountry = Loader::country($user->country);
87
        $country     = ! empty($userCountry) ? $userCountry->getName().' '.$userCountry->getEmoji() : null;
88
        $phone       = ! empty($userCountry) ? $userCountry->getCallingCode().$user->phone : null;
89
90
        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...
91
    }
92
93
    /**
94
     * Bulk control the given resources.
95
     *
96
     * @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...
97
     */
98
    public function bulk()
99
    {
100
        //
101
    }
102
103
    /**
104
     * Show the form for creating a new resource.
105
     *
106
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...\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...
107
     */
108
    public function create()
109
    {
110
        return $this->form('create', 'store');
111
    }
112
113
    /**
114
     * Show the form for copying the given resource.
115
     *
116
     * @param int $id
117
     *
118
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...\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...
119
     */
120
    public function copy($id)
121
    {
122
        return $this->form('copy', 'store', $id);
123
    }
124
125
    /**
126
     * Show the form for editing the given resource.
127
     *
128
     * @param int $id
129
     *
130
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...\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...
131
     */
132
    public function edit($id)
133
    {
134
        return $this->form('edit', 'update', $id);
135
    }
136
137
    /**
138
     * Show the form for create/edit/copy of the given resource.
139
     *
140
     * @param string   $mode
141
     * @param string   $action
142
     * @param int|null $id
143
     *
144
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...\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...
145
     */
146
    protected function form($mode, $action, $id = null)
147
    {
148
        if (! $user = $this->userRepository->getModelInstance($id)) {
0 ignored issues
show
Documentation Bug introduced by
The method getModelInstance does not exist on object<Rinvex\Fort\Contr...UserRepositoryContract>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
149
            return intend([
150
                'intended'   => route('rinvex.fort.backend.users.index'),
151
                'withErrors' => ['rinvex.fort.user.not_found' => trans('rinvex.fort::backend/messages.user.not_found', ['user' => $id])],
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...
152
            ]);
153
        }
154
155
        $countries = Loader::countries();
156
        $resources = app('rinvex.fort.ability')->findAll()->groupBy('resource');
157
158
        return view('rinvex.fort::backend.users.form', compact('user', 'resources', 'countries', 'mode', 'action'));
159
    }
160
161
    /**
162
     * Store a newly created resource in storage.
163
     *
164
     * @param \Illuminate\Http\Request $request
165
     *
166
     * @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...
167
     */
168
    public function store(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
169
    {
170
        //
171
    }
172
173
    /**
174
     * Update the given resource in storage.
175
     *
176
     * @param \Illuminate\Http\Request $request
177
     * @param int                      $id
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 update(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

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

Loading history...
182
    {
183
        //
184
    }
185
186
    /**
187
     * Delete the given resource from storage.
188
     *
189
     * @param int $id
190
     *
191
     * @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...
192
     */
193
    public function delete($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

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

Loading history...
194
    {
195
        //
196
    }
197
198
    /**
199
     * Import the given resources into storage.
200
     *
201
     * @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...
202
     */
203
    public function import()
204
    {
205
        //
206
    }
207
208
    /**
209
     * Export the given resources from storage.
210
     *
211
     * @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...
212
     */
213
    public function export()
214
    {
215
        //
216
    }
217
}
218