Completed
Push — develop ( fb66b0...dcfb70 )
by Abdelrahman
24:20 queued 21:55
created

UsersController::process()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 4
nop 2
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
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' => 'cortex-users',
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 = countries();
78
        $authUser = $request->user($this->getGuard());
79
        $languages = collect(languages())->pluck('name', 'iso_639_1');
80
        $genders = ['m' => trans('cortex/fort::common.male'), 'f' => trans('cortex/fort::common.female')];
81
82
        $roles = $authUser->isSuperadmin()
83
            ? app('rinvex.fort.role')->all()->pluck('name', 'id')->toArray()
84
            : $authUser->roles->pluck('name', 'id')->toArray();
85
86
        $abilities = $authUser->isSuperadmin()
87
            ? app('rinvex.fort.ability')->all()->groupBy('resource')->map->pluck('name', 'id')->toArray()
88
            : $authUser->allAbilities->groupBy('resource')->map->pluck('name', 'id')->toArray();
89
90
        $logs = app(LogsDataTable::class)->with(['id' => '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 144 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
        $activities = app(ActivitiesDataTable::class)->with(['id' => '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 168 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...
92
93
        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...
94
    }
95
96
    /**
97
     * Store a newly created resource in storage.
98
     *
99
     * @param \Cortex\Fort\Http\Requests\Adminarea\UserFormRequest $request
100
     *
101
     * @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...
102
     */
103
    public function store(UserFormRequest $request)
104
    {
105
        return $this->process($request, app('rinvex.fort.user'));
106
    }
107
108
    /**
109
     * Update the given resource in storage.
110
     *
111
     * @param \Cortex\Fort\Http\Requests\Adminarea\UserFormRequest $request
112
     * @param \Rinvex\Fort\Contracts\UserContract                  $user
113
     *
114
     * @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...
115
     */
116
    public function update(UserFormRequest $request, UserContract $user)
117
    {
118
        return $this->process($request, $user);
119
    }
120
121
    /**
122
     * Process the form for store/update of the given resource.
123
     *
124
     * @param \Illuminate\Foundation\Http\FormRequest $request
125
     * @param \Rinvex\Fort\Contracts\UserContract     $user
126
     *
127
     * @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...
128
     */
129
    protected function process(FormRequest $request, UserContract $user)
130
    {
131
        // Prepare required input fields
132
        $data = $request->validated();
133
134
        ! $request->hasFile('profile_picture')
135
        || $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...
136
                ->sanitizingFileName(function ($fileName) {
137
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
138
                })
139
                ->toMediaCollection('profile_picture', config('cortex.fort.media.disk'));
140
141
        ! $request->hasFile('cover_photo')
142
        || $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...
143
                ->sanitizingFileName(function ($fileName) {
144
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
145
                })
146
                ->toMediaCollection('cover_photo', config('cortex.fort.media.disk'));
147
148
        // Save user
149
        $user->fill($data)->save();
150
151
        return intend([
152
            'url' => route('adminarea.users.index'),
153
            '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...
154
        ]);
155
    }
156
157
    /**
158
     * Delete the given resource from storage.
159
     *
160
     * @param \Rinvex\Fort\Contracts\UserContract $user
161
     *
162
     * @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...
163
     */
164
    public function delete(UserContract $user)
165
    {
166
        $user->delete();
167
168
        return intend([
169
            'url' => route('adminarea.users.index'),
170
            '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...
171
        ]);
172
    }
173
174
    /**
175
     * Delete the given resource from storage.
176
     *
177
     * @param \Rinvex\Fort\Contracts\UserContract $user
178
     * @param \Spatie\MediaLibrary\Models\Media   $media
179
     *
180
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
181
     */
182
    public function deleteMedia(UserContract $user, Media $media)
183
    {
184
        $user->media()->where('id', $media->id)->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...
185
186
        return intend([
187
            'url' => route('adminarea.users.edit', ['user' => $user]),
188
            'with' => ['warning' => trans('cortex/fort::messages.user.media_deleted')],
189
        ]);
190
    }
191
}
192