PreferenceRepository::indexPreference()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 9
rs 10
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\Cache;
7
use Pratiksh\Adminetic\Contracts\PreferenceRepositoryInterface;
8
use Pratiksh\Adminetic\Http\Requests\PreferenceRequest;
9
use Pratiksh\Adminetic\Models\Admin\Preference;
10
use Pratiksh\Adminetic\Models\Admin\Role;
11
12
class PreferenceRepository implements PreferenceRepositoryInterface
13
{
14
    // Preference Index
15
    public function indexPreference()
16
    {
17
        $preferences = config('adminetic.caching', true)
18
            ? (Cache::has('preferences') ? Cache::get('preferences') : Cache::rememberForever('preferences', function () {
19
                return Preference::latest()->get();
20
            }))
21
            : Preference::latest()->get();
22
23
        return compact('preferences');
24
    }
25
26
    // Preference Create
27
    public function createPreference()
28
    {
29
        $roles = Cache::get('roles', Role::all());
30
31
        return compact('roles');
32
    }
33
34
    // Preference Store
35
    public function storePreference(PreferenceRequest $request)
36
    {
37
        $preference = Preference::create($request->validated());
38
        /* Attaching preference to user */
39
        $users = User::all();
40
        if (isset($users)) {
41
            foreach ($users as $user) {
42
                if (! isset($preference->roles)) {
43
                    $preference->users()->attach($user->id, [
44
                        'enabled' => $preference->active,
45
                    ]);
46
                } else {
47
                    if (array_intersect($user->roles->pluck('id')->toArray(), $preference->roles) != null) {
48
                        $preference->users()->attach($user->id, [
49
                            'enabled' => $preference->active,
50
                        ]);
51
                    }
52
                }
53
            }
54
        }
55
    }
56
57
    // Preference Show
58
    public function showPreference(Preference $preference)
59
    {
60
        return compact('preference');
61
    }
62
63
    // Preference Edit
64
    public function editPreference(Preference $preference)
65
    {
66
        $roles = Cache::get('roles', Role::all());
67
68
        return compact('preference', 'roles');
69
    }
70
71
    // Preference Update
72
    public function updatePreference(PreferenceRequest $request, Preference $preference)
73
    {
74
        $preference->update($request->validated());
75
        $users = User::all();
76
        $preference->users()->sync($users);
77
    }
78
79
    // Preference Destroy
80
    public function destroyPreference(Preference $preference)
81
    {
82
        // Detach Users
83
        $preference->users()->detach();
84
85
        $preference->delete();
86
    }
87
}
88