Test Setup Failed
Push — master ( 0ba822...75602f )
by guillaume
23:20 queued 17:58
created

UserRepositorySql   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 83
dl 0
loc 138
rs 10
c 1
b 0
f 0
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getById() 0 8 2
A getAdminOfOrganization() 0 12 3
A update() 0 11 1
A add() 0 9 1
A getByProvider() 0 8 2
A search() 0 45 4
A addStats() 0 5 1
A getByEmail() 0 8 2
A delete() 0 4 1
A getStats() 0 7 3
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);
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 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 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 organization_id 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);
0 ignored issues
show
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 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 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...
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 search(string $organizationId, int $page, int $perPage = 10): array
62
    {
63
        $first = DB::table('users', 'u')
64
            ->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')
65
            ->leftJoin('invitations', 'invitations.email', 'u.email')
66
            ->where('u.organization_id', $organizationId);
67
68
        $records = DB::table('users', 'u')
69
            ->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')
70
            ->rightJoin('invitations', 'invitations.email', 'u.email')
71
            ->where('invitations.organization_id', $organizationId)
72
            ->union($first)
73
            ->get()
74
        ;
75
76
        $count = $records->count();
77
78
        $records = DB::table('users', 'u')
79
            ->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')
80
            ->rightJoin('invitations', 'invitations.email', 'u.email')
81
            ->where('invitations.organization_id', $organizationId)
82
            ->union($first)
83
            ->offset(($page-1)*$perPage)
84
            ->limit($perPage)
85
            ->get();
86
        if(empty($records)){
87
            return [];
88
        }
89
90
        $users = [];
91
        foreach($records as $record){
92
            if($record->uuid === null){
93
                $identity = new Identity(Uuid::uuid4(), $record->iemail, $record->ifirstname, $record->ilastname, null);
94
                $state = new State($organizationId, null, false, []);
95
                $users[] = new UserDto($identity, $state);
96
                continue;
97
            }
98
            $roles = \App\User::find($record->uid)->roles()->pluck('name')->toArray();
99
            $identity = new Identity($record->uuid, $record->uemail, $record->ufirstname, $record->ulastname, $record->path_picture);
100
            $state = new State($organizationId, $record->last_login_at, true, $roles);
101
            $users[] = new UserDto($identity, $state);
102
        }
103
        return [
104
            'list' => $users,
105
            'total' => $count
106
        ];
107
    }
108
109
    public function delete(string $userId)
110
    {
111
        $userModel = \App\User::where('uuid', $userId)->first();
112
        $userModel->delete();
113
    }
114
115
    public function getAdminOfOrganization(string $organizationId): array
116
    {
117
        $records = \App\User::role(['admin'])->where('organization_id', $organizationId)->get();
118
        if(empty($records)){
119
            return [];
120
        }
121
        $users = [];
122
        foreach($records as $record){
123
            $roles = \App\User::find($record->id)->roles()->pluck('name')->toArray();
124
            $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 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 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 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 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...
125
        }
126
        return $users;
127
    }
128
129
    public function getByProvider(string $provider, string $providerId): ?User
130
    {
131
        $record = \App\User::where('providers->'.$provider, $providerId)->first();
132
        if(!isset($record)){
133
            return null;
134
        }
135
        $roles = $record->roles()->pluck('name')->toArray();
136
        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 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 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 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 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 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 path_picture does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
137
    }
138
139
    public function getStats(string $userId): Stats
140
    {
141
        $record = \App\User::where('uuid', $userId)->first();
142
        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...
143
            return new Stats($record->wiki_stats);
144
        }
145
        return new Stats([]);
146
    }
147
148
    public function addStats(string $userId, Stats $stats)
149
    {
150
        DB::table('users')
151
            ->where('uuid', $userId)
152
            ->update(['wiki_stats' => $stats->toArray()]);
153
    }
154
}
155