Completed
Pull Request — master (#26)
by Beñat
03:25
created

InMemoryUserRepository::userOfInvitationToken()   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
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
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.
39
     *
40
     * @var UserEventBus
41
     */
42
    private $eventBus;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param UserEventBus $anEventBus The user event bus
48
     */
49
    public function __construct(UserEventBus $anEventBus)
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 userOfEmail(UserEmail $anEmail)
69
    {
70
        foreach ($this->users as $user) {
71
            if (true === $user->email()->equals($anEmail)) {
72
                return $user;
73
            }
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function userOfConfirmationToken(UserToken $aConfirmationToken)
81
    {
82
        foreach ($this->users as $user) {
83
            if (true === $user->confirmationToken()->equals($aConfirmationToken)) {
84
                return $user;
85
            }
86
        }
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function userOfInvitationToken(UserToken $anInvitationToken)
93
    {
94
        foreach ($this->users as $user) {
95
            if (true === $user->invitationToken()->equals($anInvitationToken)) {
96
                return $user;
97
            }
98
        }
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function userOfRememberPasswordToken(UserToken $aRememberPasswordToken)
105
    {
106
        foreach ($this->users as $user) {
107
            if (true === $user->rememberPasswordToken()->equals($aRememberPasswordToken)) {
108
                return $user;
109
            }
110
        }
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 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...
117
    {
118
        $this->users[$aUser->id()->id()] = $aUser;
119
120
        foreach ($aUser->events() as $event) {
121
            $this->eventBus->handle($event);
122
        }
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 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...
129
    {
130
        unset($this->users[$aUser->id()->id()]);
131
132
        foreach ($aUser->events() as $event) {
133
            $this->eventBus->handle($event);
134
        }
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function size()
141
    {
142
        return count($this->users);
143
    }
144
}
145