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

InMemoryUserRepository::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace App\Src\UseCases\Infra\InMemory;
5
6
7
use App\Src\UseCases\Domain\Ports\UserRepository;
8
use App\Src\UseCases\Domain\User;
9
use App\Src\UseCases\Domain\Users\Stats;
10
11
class InMemoryUserRepository implements UserRepository
12
{
13
    private $users = [];
14
    private $stats = [];
15
16
    public function add(User $u, string $password = null)
17
    {
18
        $this->users[] = $u;
19
    }
20
21
    public function getByEmail(string $email):?User
22
    {
23
        foreach ($this->users as $user){
24
            if($user->email() === $email){
25
                return $user;
26
            }
27
        }
28
        return null;
29
    }
30
31
    public function getById(string $id):?User
32
    {
33
        foreach ($this->users as $user){
34
            if($user->id() === $id){
35
                return $user;
36
            }
37
        }
38
        return null;
39
    }
40
41
    public function search(string $organizationId, int $page, int $perPage = 10): array
42
    {
43
        $users = [];
44
        foreach($this->users as $user){
45
            if($user->organizationId() === $organizationId){
46
                $users[] = $user->toDto();
47
            }
48
        }
49
        $chunks = array_chunk($users, $perPage);
50
        $list = isset($chunks[$page-1]) ? $chunks[$page-1] : [];
51
        return [
52
            'list' => $list,
53
            'total' => count($users)
54
        ];
55
    }
56
57
    public function update(User $u)
58
    {
59
        foreach ($this->users as $key => $user){
60
            if($user->id() === $u->id()){
61
                $this->users[$key] = $u;
62
            }
63
        }
64
    }
65
66
    public function updateProviders(User $u)
67
    {
68
        foreach ($this->users as $key => $user){
69
            if($user->id() === $u->id()){
70
                $this->users[$key] = $u;
71
            }
72
        }
73
    }
74
75
    public function delete(string $userId)
76
    {
77
        foreach ($this->users as $key => $user){
78
            if($user->id() === $userId){
79
                unset($this->users[$key]);
80
            }
81
        }
82
    }
83
84
    public function getAdminOfOrganization(string $organizationId): array
85
    {
86
        $users = [];
87
        foreach ($this->users as $key => $user){
88
            if($user->organizationId() === $organizationId && $user->isAdmin()){
89
                $users[] = $user;
90
            }
91
        }
92
        return $users;
93
    }
94
95
    public function getByProvider(string $provider, string $providerId): ?User
96
    {
97
        foreach ($this->users as $user){
98
            if($user->provider($provider, $providerId) === true){
99
                return $user;
100
            }
101
        }
102
        return null;
103
    }
104
105
    public function getStats(string $userId): Stats
106
    {
107
        return isset($this->stats[$userId]) ? $this->stats[$userId] : new Stats([]);
108
    }
109
110
    public function addStats(string $userId, Stats $stats)
111
    {
112
        $this->stats[$userId] = $stats;
113
    }
114
}
115