UserRepository::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Http\Requests\AdminStoreRequest;
6
use App\Models\User;
7
use Carbon\Carbon;
8
use Illuminate\Support\Facades\Hash;
9
10
class UserRepository implements UserRepositoryInterface
11
{
12
13
    /**
14
     * Get all the users.
15
     *
16
     * @param int|null $recordsPerPage
17
     * @param array|null $searchParameters
18
     *
19
     * @return iterable|\Illuminate\Contracts\Pagination\LengthAwarePaginator
20
     */
21 1
    public function users(int $recordsPerPage = null, array $searchParameters = null)
22
    {
23 1
        $query = User::orderBy('email', 'asc');
24 1
        $query->with('profile');
25
26 1
        if (!is_null($searchParameters)) {
27
            if (!empty($searchParameters['name'])) {
28
                $query->whereHas('profile', function ($q) use ($searchParameters) {
29
                    $q->where('name', 'like', '%' . $searchParameters['name'] . '%');
30
                });
31
            }
32
            if (!empty($searchParameters['surname'])) {
33
                $query->whereHas('profile', function ($q) use ($searchParameters) {
34
                    $q->where('surname', 'like', '%' . $searchParameters['surname'] . '%');
35
                });
36
            }
37
            if (!empty($searchParameters['email'])) {
38
                $query->where('email', 'like', '%' . $searchParameters['email'] . '%');
39
            }
40
            if (!empty($searchParameters['countryId'])) {
41
                $query->whereHas('profile', function ($q) use ($searchParameters) {
42
                    $q->where('country_id', $searchParameters['countryId']);
43
                });
44
            }
45
            if (!empty($searchParameters['role'])) {
46
                $query->role($searchParameters['role']);
47
            }
48
            if (!empty($searchParameters['team'])) {
49
                $query->role($searchParameters['team']);
50
            }
51
            if (!empty($searchParameters['status'])) {
52
                $query->currentStatus($searchParameters['status']);
53
            }
54
        }
55
56 1
        if ($recordsPerPage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $recordsPerPage of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
57 1
            return $query->paginate($recordsPerPage)->withQueryString();
58
        }
59
        return $query->get();
60
    }
61
62
    /**
63
     * Get user by id
64
     *
65
     * @param int $userId
66
     * @return User
67
     */
68 4
    public function getById(int $userId): User
69
    {
70 4
        return User::findOrFail($userId);
71
    }
72
73
    /**
74
     * Store User
75
     *
76
     * @param array $data
77
     * @return User
78
     */
79 1
    public function storeUser(array $data): User
80
    {
81 1
        $user = new User();
82
83 1
        $user->email = $data['email'];
84 1
        $user->password = (array_key_exists('password', $data)) ? Hash::make($data['password']) : null;
85
86 1
        $user->save();
87
88 1
        $user->setStatus('enabled');
89
90 1
        return $user->fresh();
91
    }
92
93
    /**
94
     * Update User
95
     *
96
     * @param array $data
97
     * @param int $userId
98
     * @return User
99
     */
100 1
    public function update(array $data, int $userId): User
101
    {
102 1
        $user = $this->getById($userId);
103
104 1
        $user->email = $data['email'];
105 1
        if (array_key_exists('password', $data)) {
106 1
            $user->password = Hash::make($data['password']);
107
        }
108
109 1
        $user->update();
110
111 1
        $status = (isset($data['status'])) ? 'enabled' : 'disabled';
112 1
        if ($user->status() != $status) {
113 1
            $user->setStatus($status);
114
        }
115
116 1
        return $user;
117
    }
118
119
    /**
120
     * Delete User
121
     *
122
     * @param int $userId
123
     * @return void
124
     */
125 1
    public function delete(int $userId): void
126
    {
127 1
        User::destroy($userId);
128 1
    }
129
}
130