Completed
Push — master ( 33476c...427ebd )
by Bertrand
09:14
created

UserRepositorySql::buildUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 11
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
4
namespace App\Src\UseCases\Infra\Sql;
5
6
use App\Src\UseCases\Domain\Ports\UserRepository;
7
use App\Src\UseCases\Domain\User;
8
use App\Src\UseCases\Domain\Users\Identity;
9
use App\Src\UseCases\Domain\Users\State;
10
use App\Src\UseCases\Domain\Users\Stats;
11
use App\Src\UseCases\Domain\Users\UserDto;
12
use Illuminate\Support\Facades\DB;
13
use Ramsey\Uuid\Uuid;
14
15
class UserRepositorySql implements UserRepository
16
{
17
    public function getByEmail(string $email): ?User
18
    {
19
        $record = \App\User::where('email', $email)->first();
20
        if(!isset($record)){
21
            return null;
22
        }
23
        $roles = $record->roles()->pluck('name')->toArray();
24
        return $this->buildUser($record, $roles);
25
    }
26
27
    public function getById(string $id): ?User
28
    {
29
        $record = \App\User::where('uuid', $id)->first();
30
        if(!isset($record)){
31
            return null;
32
        }
33
        $roles = $record->roles()->pluck('name')->toArray();
34
        return $this->buildUser($record, $roles);
35
    }
36
37
    public function add(User $u, string $password = null)
38
    {
39
        $userModel = new \App\User();
40
        $data = $u->toArray();
41
        $roles = $data['roles'];
42
        unset($data['url_picture']);
43
        unset($data['roles']);
44
        $userModel->fill($data);
45
        $userModel->password = $password;
46
        $userModel->save();
47
48
        $userModel->syncRoles($roles);
49
    }
50
51
    public function update(User $u)
52
    {
53
        $userModel = \App\User::where('uuid', $u->id())->first();
54
        $data = $u->toArray();
55
        $roles = $data['roles'];
56
        unset($data['url_picture']);
57
        unset($data['roles']);
58
        $userModel->fill($data);
59
        $userModel->save();
60
61
        $userModel->syncRoles($roles);
62
    }
63
64
    public function updateProviders(User $u)
65
    {
66
        $userModel = \App\User::where('uuid', $u->id())->first();
67
        $providers = $u->toArray()['providers'];
68
        $userModel->providers = $providers;
69
        $userModel->save();
70
    }
71
72
73
    public function search(string $organizationId, int $page, int $perPage = 10): array
74
    {
75
        $first = DB::table('users', 'u')
76
            ->selectRaw('u.last_login_at, u.path_picture, u.uuid, u.id as uid, u.firstname as ufirstname, u.lastname as ulastname, u.email as uemail, invitations.id as iid, invitations.firstname as ifirstname, invitations.lastname as ilastname, invitations.email as iemail')
77
            ->leftJoin('invitations', 'invitations.email', 'u.email')
78
            ->where('u.organization_id', $organizationId);
79
80
        $records = DB::table('users', 'u')
81
            ->selectRaw('u.last_login_at, u.path_picture, u.uuid, u.id as uid, u.firstname as ufirstname, u.lastname as ulastname, u.email as uemail, invitations.id as iid, invitations.firstname as ifirstname, invitations.lastname as ilastname, invitations.email as iemail')
82
            ->rightJoin('invitations', 'invitations.email', 'u.email')
83
            ->where('invitations.organization_id', $organizationId)
84
            ->union($first)
85
            ->get()
86
        ;
87
88
        $count = $records->count();
89
90
        $records = DB::table('users', 'u')
91
            ->selectRaw('u.last_login_at, u.path_picture, u.uuid, u.id as uid, u.firstname as ufirstname, u.lastname as ulastname, u.email as uemail, invitations.id as iid, invitations.firstname as ifirstname, invitations.lastname as ilastname, invitations.email as iemail')
92
            ->rightJoin('invitations', 'invitations.email', 'u.email')
93
            ->where('invitations.organization_id', $organizationId)
94
            ->union($first)
95
            ->offset(($page-1)*$perPage)
96
            ->limit($perPage)
97
            ->get();
98
        if(empty($records)){
99
            return [];
100
        }
101
102
        $users = [];
103
        foreach($records as $record){
104
            if($record->uuid === null){
105
                $identity = new Identity(Uuid::uuid4(), $record->iemail, $record->ifirstname, $record->ilastname, null);
106
                $state = new State($organizationId, null, false, []);
107
                $users[] = new UserDto($identity, $state);
108
                continue;
109
            }
110
            $roles = \App\User::find($record->uid)->roles()->pluck('name')->toArray();
111
            $identity = new Identity($record->uuid, $record->uemail, $record->ufirstname, $record->ulastname, $record->path_picture);
112
            $state = new State($organizationId, $record->last_login_at, true, $roles);
113
            $users[] = new UserDto($identity, $state);
114
        }
115
        return [
116
            'list' => $users,
117
            'total' => $count
118
        ];
119
    }
120
121
    public function delete(string $userId)
122
    {
123
        $userModel = \App\User::where('uuid', $userId)->first();
124
        $userModel->delete();
125
    }
126
127
    public function getAdminOfOrganization(string $organizationId): array
128
    {
129
        $records = \App\User::role(['admin'])->where('organization_id', $organizationId)->get();
130
        if(empty($records)){
131
            return [];
132
        }
133
        $users = [];
134
        foreach($records as $record){
135
            $roles = \App\User::find($record->id)->roles()->pluck('name')->toArray();
136
            $users[] = new User($record->uuid, $record->email, $record->firstname, $record->lastname, $record->organization_id, $record->path_picture, $roles);
137
        }
138
        return $users;
139
    }
140
141
    public function getByProvider(string $provider, string $providerId): ?User
142
    {
143
        $record = \App\User::where('providers->'.$provider, $providerId)->first();
144
        if(!isset($record)){
145
            return null;
146
        }
147
        $roles = $record->roles()->pluck('name')->toArray();
148
        return new User($record->uuid, $record->email, $record->firstname, $record->lastname, $record->organization_id, $record->path_picture, $roles, $record->providers);
149
    }
150
151
    public function getStats(string $userId): Stats
152
    {
153
        $record = \App\User::where('uuid', $userId)->first();
154
        if(isset($record) && $record->wiki_stats !== null){
155
            return new Stats($record->wiki_stats);
156
        }
157
        return new Stats([]);
158
    }
159
160
    public function addStats(string $userId, Stats $stats)
161
    {
162
        DB::table('users')
163
            ->where('uuid', $userId)
164
            ->update(['wiki_stats' => $stats->toArray()]);
165
    }
166
167
    /**
168
     * @param $record
169
     * @param $roles
170
     * @return User
171
     */
172
    private function buildUser($record, $roles): User
173
    {
174
        return new User(
175
            $record->uuid,
176
            $record->email,
177
            $record->firstname,
178
            $record->lastname,
179
            $record->organization_id,
180
            $record->path_picture,
181
            $roles,
182
            $record->providers ?? []
183
        );
184
    }
185
}
186