Completed
Pull Request — master (#44)
by Beñat
02:18
created

InMemoryUserRepository::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\User\Infrastructure\Persistence;
14
15
use BenGorUser\User\Domain\Model\User;
16
use BenGorUser\User\Domain\Model\UserEmail;
17
use BenGorUser\User\Domain\Model\UserId;
18
use BenGorUser\User\Domain\Model\UserRepository;
19
use BenGorUser\User\Domain\Model\UserToken;
20
use BenGorUser\User\Infrastructure\Domain\Model\UserEventBus;
21
22
/**
23
 * In memory user repository class.
24
 *
25
 * @author Beñat Espiña <[email protected]>
26
 * @author Gorka Laucirica <[email protected]>
27
 */
28
final class InMemoryUserRepository implements UserRepository
29
{
30
    /**
31
     * Array which contains the users.
32
     *
33
     * @var array
34
     */
35
    private $users;
36
37
    /**
38
     * The user event bus, it can be null.
39
     *
40
     * @var UserEventBus|null
41
     */
42
    private $eventBus;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param UserEventBus|null $anEventBus The user event bus, it can be null
48
     */
49
    public function __construct(UserEventBus $anEventBus = null)
50
    {
51
        $this->users = [];
52
        $this->eventBus = $anEventBus;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function userOfId(UserId $anId)
59
    {
60
        if (isset($this->users[$anId->id()])) {
61
            return $this->users[$anId->id()];
62
        }
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function all()
69
    {
70
        return $this->users;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function userOfEmail(UserEmail $anEmail)
77
    {
78
        foreach ($this->users as $user) {
79
            if (true === $user->email()->equals($anEmail)) {
80
                return $user;
81
            }
82
        }
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function userOfConfirmationToken(UserToken $aConfirmationToken)
89
    {
90
        foreach ($this->users as $user) {
91
            if (true === $user->confirmationToken()->equals($aConfirmationToken)) {
92
                return $user;
93
            }
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function userOfInvitationToken(UserToken $anInvitationToken)
101
    {
102
        foreach ($this->users as $user) {
103
            if (true === $user->invitationToken()->equals($anInvitationToken)) {
104
                return $user;
105
            }
106
        }
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function userOfRememberPasswordToken(UserToken $aRememberPasswordToken)
113
    {
114
        foreach ($this->users as $user) {
115
            if (true === $user->rememberPasswordToken()->equals($aRememberPasswordToken)) {
116
                return $user;
117
            }
118
        }
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 View Code Duplication
    public function persist(User $aUser)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        $this->users[$aUser->id()->id()] = $aUser;
127
128
        if ($this->eventBus instanceof UserEventBus) {
129
            $this->handle($aUser->events());
130
        }
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 View Code Duplication
    public function remove(User $aUser)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        unset($this->users[$aUser->id()->id()]);
139
140
        if ($this->eventBus instanceof UserEventBus) {
141
            $this->handle($aUser->events());
142
        }
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function size()
149
    {
150
        return count($this->users);
151
    }
152
153
    /**
154
     * Handles the given events with event bus.
155
     *
156
     * @param array $events A collection of user domain events
157
     */
158
    private function handle($events)
159
    {
160
        foreach ($events as $event) {
161
            $this->eventBus->handle($event);
162
        }
163
    }
164
}
165