Completed
Push — develop ( 60af44...903ade )
by Abdelrahman
01:52
created

UsersController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 181
rs 10
wmc 17
lcom 1
cbo 6

9 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 7 1
A logs() 0 6 3
A activities() 0 6 3
B form() 0 26 3
A store() 0 4 1
A update() 0 4 1
B process() 0 27 3
A delete() 0 9 1
A deleteMedia() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Controllers\Adminarea;
6
7
use Illuminate\Http\Request;
8
use Spatie\MediaLibrary\Models\Media;
9
use Rinvex\Fort\Contracts\UserContract;
10
use Illuminate\Foundation\Http\FormRequest;
11
use Cortex\Foundation\DataTables\LogsDataTable;
12
use Cortex\Fort\DataTables\Adminarea\UsersDataTable;
13
use Cortex\Foundation\DataTables\ActivitiesDataTable;
14
use Cortex\Fort\Http\Requests\Adminarea\UserFormRequest;
15
use Cortex\Foundation\Http\Controllers\AuthorizedController;
16
17
class UsersController extends AuthorizedController
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $resource = 'users';
23
24
    /**
25
     * Display a listing of the resource.
26
     *
27
     * @param \Cortex\Fort\DataTables\Adminarea\UsersDataTable $usersDataTable
28
     *
29
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
30
     */
31
    public function index(UsersDataTable $usersDataTable)
32
    {
33
        return $usersDataTable->with([
34
            'id' => 'adminarea-users-index-table',
35
            'phrase' => trans('cortex/fort::common.users'),
36
        ])->render('cortex/foundation::adminarea.pages.datatable');
37
    }
38
39
    /**
40
     * Get a listing of the resource logs.
41
     *
42
     * @param \Rinvex\Fort\Contracts\UserContract $user
43
     *
44
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
45
     */
46
    public function logs(UserContract $user)
47
    {
48
        return request()->ajax() && request()->wantsJson()
49
            ? app(LogsDataTable::class)->with(['resource' => $user])->ajax()
50
            : intend(['url' => route('adminarea.users.edit', ['user' => $user]).'#logs-tab']);
51
    }
52
53
    /**
54
     * Get a listing of the resource activities.
55
     *
56
     * @param \Rinvex\Fort\Contracts\UserContract $user
57
     *
58
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
59
     */
60
    public function activities(UserContract $user)
61
    {
62
        return request()->ajax() && request()->wantsJson()
63
            ? app(ActivitiesDataTable::class)->with(['resource' => $user])->ajax()
64
            : intend(['url' => route('adminarea.users.edit', ['user' => $user]).'#activities-tab']);
65
    }
66
67
    /**
68
     * Show the form for create/update of the given resource.
69
     *
70
     * @param \Illuminate\Http\Request            $request
71
     * @param \Rinvex\Fort\Contracts\UserContract $user
72
     *
73
     * @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...
74
     */
75
    public function form(Request $request, UserContract $user)
76
    {
77
        $countries = collect(countries())->map(function ($country, $code) {
78
            return [
79
                'id' => $code,
80
                'text' => $country['name'],
81
                'emoji' => $country['emoji'],
82
            ];
83
        })->values();
84
        $authUser = $request->user($this->getGuard());
85
        $languages = collect(languages())->pluck('name', 'iso_639_1');
86
        $genders = ['m' => trans('cortex/fort::common.male'), 'f' => trans('cortex/fort::common.female')];
87
88
        $roles = $authUser->isSuperadmin()
89
            ? app('rinvex.fort.role')->all()->pluck('name', 'id')->toArray()
90
            : $authUser->roles->pluck('name', 'id')->toArray();
91
92
        $abilities = $authUser->isSuperadmin()
93
            ? app('rinvex.fort.ability')->all()->groupBy('resource')->map->pluck('name', 'id')->toArray()
94
            : $authUser->allAbilities->groupBy('resource')->map->pluck('name', 'id')->toArray();
95
96
        $logs = app(LogsDataTable::class)->with(['id' => "adminarea-users-{$user->getKey()}-logs-table"])->html()->minifiedAjax(route('adminarea.users.logs', ['user' => $user]));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 178 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...
97
        $activities = app(ActivitiesDataTable::class)->with(['id' => "adminarea-users-{$user->getKey()}-activities-table"])->html()->minifiedAjax(route('adminarea.users.activities', ['user' => $user]));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 202 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...
98
99
        return view('cortex/fort::adminarea.pages.user', compact('user', 'abilities', 'roles', 'countries', 'languages', 'genders', 'logs', 'activities'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 155 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...
100
    }
101
102
    /**
103
     * Store a newly created resource in storage.
104
     *
105
     * @param \Cortex\Fort\Http\Requests\Adminarea\UserFormRequest $request
106
     *
107
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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 store(UserFormRequest $request)
110
    {
111
        return $this->process($request, app('rinvex.fort.user'));
112
    }
113
114
    /**
115
     * Update the given resource in storage.
116
     *
117
     * @param \Cortex\Fort\Http\Requests\Adminarea\UserFormRequest $request
118
     * @param \Rinvex\Fort\Contracts\UserContract                  $user
119
     *
120
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
121
     */
122
    public function update(UserFormRequest $request, UserContract $user)
123
    {
124
        return $this->process($request, $user);
125
    }
126
127
    /**
128
     * Process the form for store/update of the given resource.
129
     *
130
     * @param \Illuminate\Foundation\Http\FormRequest $request
131
     * @param \Rinvex\Fort\Contracts\UserContract     $user
132
     *
133
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
134
     */
135
    protected function process(FormRequest $request, UserContract $user)
136
    {
137
        // Prepare required input fields
138
        $data = $request->validated();
139
140
        ! $request->hasFile('profile_picture')
141
        || $user->addMediaFromRequest('profile_picture')
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Rinvex\Fort\Contracts\UserContract as the method addMediaFromRequest() does only exist in the following implementations of said interface: Cortex\Fort\Models\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
142
                ->sanitizingFileName(function ($fileName) {
143
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
144
                })
145
                ->toMediaCollection('profile_picture', config('cortex.fort.media.disk'));
146
147
        ! $request->hasFile('cover_photo')
148
        || $user->addMediaFromRequest('cover_photo')
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Rinvex\Fort\Contracts\UserContract as the method addMediaFromRequest() does only exist in the following implementations of said interface: Cortex\Fort\Models\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
149
                ->sanitizingFileName(function ($fileName) {
150
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
151
                })
152
                ->toMediaCollection('cover_photo', config('cortex.fort.media.disk'));
153
154
        // Save user
155
        $user->fill($data)->save();
156
157
        return intend([
158
            'url' => route('adminarea.users.index'),
159
            'with' => ['success' => trans('cortex/fort::messages.user.saved', ['username' => $user->username])],
0 ignored issues
show
Bug introduced by
Accessing username on the interface Rinvex\Fort\Contracts\UserContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
160
        ]);
161
    }
162
163
    /**
164
     * Delete the given resource from storage.
165
     *
166
     * @param \Rinvex\Fort\Contracts\UserContract $user
167
     *
168
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
169
     */
170
    public function delete(UserContract $user)
171
    {
172
        $user->delete();
173
174
        return intend([
175
            'url' => route('adminarea.users.index'),
176
            'with' => ['warning' => trans('cortex/fort::messages.user.deleted', ['username' => $user->username])],
0 ignored issues
show
Bug introduced by
Accessing username on the interface Rinvex\Fort\Contracts\UserContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
177
        ]);
178
    }
179
180
    /**
181
     * Delete the given resource from storage.
182
     *
183
     * @param \Rinvex\Fort\Contracts\UserContract $user
184
     * @param \Spatie\MediaLibrary\Models\Media   $media
185
     *
186
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
187
     */
188
    public function deleteMedia(UserContract $user, Media $media)
189
    {
190
        $user->media()->where($media->getKeyName(), $media->getKey())->first()->delete();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Rinvex\Fort\Contracts\UserContract as the method media() does only exist in the following implementations of said interface: Cortex\Fort\Models\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
191
192
        return intend([
193
            'url' => route('adminarea.users.edit', ['user' => $user]),
194
            'with' => ['warning' => trans('cortex/fort::messages.user.media_deleted')],
195
        ]);
196
    }
197
}
198