Test Setup Failed
Push — master ( cbc6b9...b0c301 )
by guillaume
09:28 queued 04:37
created

InMemoryUserRepository::getByEmail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
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
10
class InMemoryUserRepository implements UserRepository
11
{
12
    private $users = [];
13
14
    public function add(User $u, string $password = null)
15
    {
16
        $this->users[] = $u;
17
    }
18
19
    public function getByEmail(string $email):?User
20
    {
21
        foreach ($this->users as $user){
22
            if($user->email() === $email){
23
                return $user;
24
            }
25
        }
26
        return null;
27
    }
28
29
    public function getById(string $id):?User
30
    {
31
        foreach ($this->users as $user){
32
            if($user->id() === $id){
33
                return $user;
34
            }
35
        }
36
        return null;
37
    }
38
39
    public function search(string $organizationId, int $page, int $perPage = 10): array
40
    {
41
        $users = [];
42
        foreach($this->users as $user){
43
            if($user->organizationId() === $organizationId){
44
                $users[] = $user;
45
            }
46
        }
47
        $chunks = array_chunk($users, $perPage);
48
        $list = isset($chunks[$page-1]) ? $chunks[$page-1] : [];
49
        return [
50
            'list' => $list,
51
            'total' => count($users)
52
        ];
53
    }
54
55
    public function update(User $u)
56
    {
57
        foreach ($this->users as $key => $user){
58
            if($user->id() === $u->id()){
59
                $this->users[$key] = $u;
60
            }
61
        }
62
    }
63
64
    public function delete(string $userId)
65
    {
66
        foreach ($this->users as $key => $user){
67
            if($user->id() === $userId){
68
                unset($this->users[$key]);
69
            }
70
        }
71
    }
72
73
    public function getAdminOfOrganization(string $organizationId): array
74
    {
75
        $users = [];
76
        foreach ($this->users as $key => $user){
77
            if($user->organizationId() === $organizationId && $user->isAdmin()){
78
                $users[] = $user;
79
            }
80
        }
81
        return $users;
82
    }
83
84
85
}
86