Test Setup Failed
Push — master ( b0c301...fb80ed )
by guillaume
01:15 queued 13s
created

UserRepositorySql   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
dl 0
loc 92
rs 10
c 1
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getById() 0 8 2
A update() 0 11 1
A add() 0 9 1
A search() 0 25 3
A getByEmail() 0 8 2
A getAdminOfOrganization() 0 12 3
A delete() 0 4 1
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 Illuminate\Support\Facades\DB;
9
10
class UserRepositorySql implements UserRepository
11
{
12
    public function getByEmail(string $email): ?User
13
    {
14
        $record = \App\User::where('email', $email)->first();
15
        if(!isset($record)){
16
            return null;
17
        }
18
        $roles = $record->roles()->pluck('name')->toArray();
19
        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 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...
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...
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...
20
    }
21
22
    public function getById(string $id): ?User
23
    {
24
        $record = \App\User::where('uuid', $id)->first();
25
        if(!isset($record)){
26
            return null;
27
        }
28
        $roles = $record->roles()->pluck('name')->toArray();
29
        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 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...
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 email does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
30
    }
31
32
    public function add(User $u, string $password = null)
33
    {
34
        $userModel = new \App\User();
35
        $data = $u->toArray();
36
        unset($data['url_picture']);
37
        unset($data['roles']);
38
        $userModel->fill($data);
39
        $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...
40
        $userModel->save();
41
    }
42
43
    public function update(User $u)
44
    {
45
        $userModel = \App\User::where('uuid', $u->id())->first();
46
        $data = $u->toArray();
47
        $roles = $data['roles'];
48
        unset($data['url_picture']);
49
        unset($data['roles']);
50
        $userModel->fill($data);
51
        $userModel->save();
52
53
        $userModel->syncRoles($roles);
54
    }
55
56
    public function search(string $organizationId, int $page, int $perPage = 10): array
57
    {
58
        $records = DB::table('users')
59
            ->select()
60
            ->where('organization_id', $organizationId)
61
        ;
62
        $count = $records->count();
63
64
        $records = DB::table('users')
65
            ->select()
66
            ->where('organization_id', $organizationId)
67
            ->offset(($page-1)*$perPage)
68
            ->limit($perPage)
69
            ->get();
70
        if(empty($records)){
71
            return [];
72
        }
73
        $users = [];
74
        foreach($records as $record){
75
            $roles = \App\User::find($record->id)->roles()->pluck('name')->toArray();
76
            $users[] = new User($record->uuid, $record->email, $record->firstname, $record->lastname, $record->organization_id, $record->path_picture, $roles);
77
        }
78
        return [
79
            'list' => $users,
80
            'total' => $count
81
        ];
82
    }
83
84
    public function delete(string $userId)
85
    {
86
        $userModel = \App\User::where('uuid', $userId)->first();
87
        $userModel->delete();
88
    }
89
90
    public function getAdminOfOrganization(string $organizationId): array
91
    {
92
        $records = \App\User::role(['admin'])->where('organization_id', $organizationId)->get();
93
        if(empty($records)){
94
            return [];
95
        }
96
        $users = [];
97
        foreach($records as $record){
98
            $roles = \App\User::find($record->id)->roles()->pluck('name')->toArray();
99
            $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 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 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 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...
100
        }
101
        return $users;
102
    }
103
}
104