UserRepository::attachPreference()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 1
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Pratiksh\Adminetic\Repositories;
4
5
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Cache;
8
use Illuminate\Support\Facades\Hash;
9
use Pratiksh\Adminetic\Contracts\UserRepositoryInterface;
10
use Pratiksh\Adminetic\Events\UserHasBeenRegistered;
11
use Pratiksh\Adminetic\Http\Requests\UserRequest;
12
use Pratiksh\Adminetic\Models\Admin\Preference;
13
use Pratiksh\Adminetic\Models\Admin\Role;
14
15
class UserRepository implements UserRepositoryInterface
16
{
17
    // User Index
18
    public function userIndex()
19
    {
20
        return [];
21
    }
22
23
    // User Create
24
    public function userCreate()
25
    {
26
        $roles = Cache::get('roles', Role::all(['id', 'name']));
27
28
        return compact('roles');
29
    }
30
31
    // User Store
32
    public function userStore(UserRequest $request)
33
    {
34
        // Validating Request
35
        $request->validated();
36
        // Creating User
37
        $user = User::create([
38
            'name' => $request->name,
39
            'email' => $request->email,
40
            'password' => Hash::make($request->password),
41
        ]);
42
        // User Registered Event
43
        event(new UserHasBeenRegistered($user, $request->password));
44
        // Asigning Role
45
        $this->assignRole($user);
46
        // Creating Profile
47
        $user->profile()->create();
48
        // Creating Preference
49
        $this->attachPreference($user);
50
    }
51
52
    // User Show
53
    public function userShow(User $user)
54
    {
55
        $profile = $user->profile;
56
57
        return compact('user', 'profile');
58
    }
59
60
    // User Edit
61
    public function userEdit(User $user)
62
    {
63
        $roles = Cache::get('roles', Role::all(['id', 'name']));
64
65
        return compact('user', 'roles');
66
    }
67
68
    public function userUpdate(UserRequest $request, User $user)
69
    {
70
        if ($request->password) {
71
            $user->update([
72
                'name' => $request->name,
73
                'email' => $request->email,
74
                'password' => Hash::make($request->password),
75
            ]);
76
        } else {
77
            $user->update([
78
                'name' => $request->name,
79
                'email' => $request->email,
80
                'password' => $user->password,
81
            ]);
82
        }
83
        $this->syncRole($user);
84
    }
85
86
    public function userDestroy(User $user)
87
    {
88
        // Deleting Profile
89
        $user->profile()->delete();
90
        // Deleting Attached Role
91
        $user->roles()->detach();
92
        // Deleting Attached Preference
93
        $user->preferences()->detach();
94
        $user->delete();
95
    }
96
97
    public function userEditRoute($user)
98
    {
99
        if (Auth::user()->id == $user->id) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
100
            return view('adminetic::admin.profile.edit', compact('user'));
101
        } else {
102
            return view('adminetic::admin.user.edit', $this->userEdit($user));
103
        }
104
    }
105
106
    public function assignRole($user)
107
    {
108
        if (request()->role) {
109
            $user->roles()->attach(request()->role);
110
        }
111
    }
112
113
    public function syncRole($user)
114
    {
115
        if (request()->role) {
116
            $role = Role::where('id', request()->role)->first();
117
            $user->roles()->sync($role);
118
        }
119
    }
120
121
    public function attachPreference($user)
122
    {
123
        $preferences = Preference::all();
124
        if (isset($preferences)) {
125
            foreach ($preferences as $preference) {
126
                if (! isset($preference->roles)) {
127
                    $user->preferences()->attach($preference->id, [
128
                        'enabled' => $preference->active,
129
                    ]);
130
                } else {
131
                    if (array_intersect($user->roles->pluck('id')->toArray(), $preference->roles) != null) {
132
                        $user->preferences()->attach($preference->id, [
133
                            'enabled' => $preference->active,
134
                        ]);
135
                    }
136
                }
137
            }
138
        }
139
    }
140
}
141