Completed
Push — master ( 029fc1...afa99c )
by guillaume
12:29 queued 06:09
created

UserRepositorySql::updateProviders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
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 new User($record->uuid, $record->email, $record->firstname, $record->lastname, $record->organization_id, $record->path_picture, $roles, $record->providers);
0 ignored issues
show
Bug Best Practice introduced by
The property path_picture does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property lastname does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property email does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property firstname does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uuid does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property organization_id does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property providers does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
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 new User($record->uuid, $record->email, $record->firstname, $record->lastname, $record->organization_id, $record->path_picture, $roles, $record->providers);
0 ignored issues
show
Bug Best Practice introduced by
The property path_picture does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uuid does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property organization_id does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property lastname does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property email does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property firstname does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property providers does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
35
    }
36
37
    public function add(User $u, string $password = null)
38
    {
39
        $userModel = new \App\User();
40
        $data = $u->toArray();
41
        unset($data['url_picture']);
42
        unset($data['roles']);
43
        $userModel->fill($data);
44
        $userModel->password = $password;
0 ignored issues
show
Bug Best Practice introduced by
The property password does not exist on App\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
45
        $userModel->save();
46
    }
47
48
    public function update(User $u)
49
    {
50
        $userModel = \App\User::where('uuid', $u->id())->first();
51
        $data = $u->toArray();
52
        $roles = $data['roles'];
53
        unset($data['url_picture']);
54
        unset($data['roles']);
55
        $userModel->fill($data);
56
        $userModel->save();
57
58
        $userModel->syncRoles($roles);
59
    }
60
61
    public function updateProviders(User $u)
62
    {
63
        $userModel = \App\User::where('uuid', $u->id())->first();
64
        $providers = $u->toArray()['providers'];
65
        $userModel->providers = $providers;
0 ignored issues
show
Bug Best Practice introduced by
The property providers does not exist on App\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
66
        $userModel->save();
67
    }
68
69
70
    public function search(string $organizationId, int $page, int $perPage = 10): array
71
    {
72
        $first = DB::table('users', 'u')
73
            ->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')
74
            ->leftJoin('invitations', 'invitations.email', 'u.email')
75
            ->where('u.organization_id', $organizationId);
76
77
        $records = DB::table('users', 'u')
78
            ->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')
79
            ->rightJoin('invitations', 'invitations.email', 'u.email')
80
            ->where('invitations.organization_id', $organizationId)
81
            ->union($first)
82
            ->get()
83
        ;
84
85
        $count = $records->count();
86
87
        $records = DB::table('users', 'u')
88
            ->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')
89
            ->rightJoin('invitations', 'invitations.email', 'u.email')
90
            ->where('invitations.organization_id', $organizationId)
91
            ->union($first)
92
            ->offset(($page-1)*$perPage)
93
            ->limit($perPage)
94
            ->get();
95
        if(empty($records)){
96
            return [];
97
        }
98
99
        $users = [];
100
        foreach($records as $record){
101
            if($record->uuid === null){
102
                $identity = new Identity(Uuid::uuid4(), $record->iemail, $record->ifirstname, $record->ilastname, null);
103
                $state = new State($organizationId, null, false, []);
104
                $users[] = new UserDto($identity, $state);
105
                continue;
106
            }
107
            $roles = \App\User::find($record->uid)->roles()->pluck('name')->toArray();
108
            $identity = new Identity($record->uuid, $record->uemail, $record->ufirstname, $record->ulastname, $record->path_picture);
109
            $state = new State($organizationId, $record->last_login_at, true, $roles);
110
            $users[] = new UserDto($identity, $state);
111
        }
112
        return [
113
            'list' => $users,
114
            'total' => $count
115
        ];
116
    }
117
118
    public function delete(string $userId)
119
    {
120
        $userModel = \App\User::where('uuid', $userId)->first();
121
        $userModel->delete();
122
    }
123
124
    public function getAdminOfOrganization(string $organizationId): array
125
    {
126
        $records = \App\User::role(['admin'])->where('organization_id', $organizationId)->get();
127
        if(empty($records)){
128
            return [];
129
        }
130
        $users = [];
131
        foreach($records as $record){
132
            $roles = \App\User::find($record->id)->roles()->pluck('name')->toArray();
133
            $users[] = new User($record->uuid, $record->email, $record->firstname, $record->lastname, $record->organization_id, $record->path_picture, $roles);
0 ignored issues
show
Bug Best Practice introduced by
The property firstname does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property path_picture does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property organization_id does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property lastname does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uuid does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property email does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
134
        }
135
        return $users;
136
    }
137
138
    public function getByProvider(string $provider, string $providerId): ?User
139
    {
140
        $record = \App\User::where('providers->'.$provider, $providerId)->first();
141
        if(!isset($record)){
142
            return null;
143
        }
144
        $roles = $record->roles()->pluck('name')->toArray();
145
        return new User($record->uuid, $record->email, $record->firstname, $record->lastname, $record->organization_id, $record->path_picture, $roles, $record->providers);
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property path_picture does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property providers does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property organization_id does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uuid does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property lastname does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property firstname does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
146
    }
147
148
    public function getStats(string $userId): Stats
149
    {
150
        $record = \App\User::where('uuid', $userId)->first();
151
        if(isset($record) && $record->wiki_stats !== null){
0 ignored issues
show
Bug Best Practice introduced by
The property wiki_stats does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
152
            return new Stats($record->wiki_stats);
153
        }
154
        return new Stats([]);
155
    }
156
157
    public function addStats(string $userId, Stats $stats)
158
    {
159
        DB::table('users')
160
            ->where('uuid', $userId)
161
            ->update(['wiki_stats' => $stats->toArray()]);
162
    }
163
}
164