1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\User; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class InMemoryUserRepository |
9
|
|
|
*/ |
10
|
|
|
class InMemoryUserRepository implements UserRepository, PasswordResetRepository |
11
|
|
|
{ |
12
|
|
|
/** @var array */ |
13
|
|
|
private $users = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* InMemoryUserRepository constructor. |
17
|
|
|
*/ |
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$user = new User( |
21
|
|
|
'wouter', |
22
|
|
|
'test', |
23
|
|
|
'Wouter Sioen', |
24
|
|
|
'[email protected]', |
25
|
|
|
1 |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$this->users[] = $user; |
29
|
|
|
|
30
|
|
|
$passwordResetUser = new User( |
31
|
|
|
'reset', |
32
|
|
|
'reset', |
33
|
|
|
'reset', |
34
|
|
|
'[email protected]', |
35
|
|
|
2, |
36
|
|
|
PasswordResetToken::generate() |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
$this->users[] = $passwordResetUser; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function findByUsername($username) |
46
|
|
|
{ |
47
|
|
|
foreach ($this->users as $user) { |
48
|
|
|
if ($user->getUsername() === $username) { |
49
|
|
|
return $user; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function find($id) |
58
|
|
|
{ |
59
|
|
|
foreach ($this->users as $user) { |
60
|
|
|
if ($user->getId() === $id) { |
61
|
|
|
return $user; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
*/ |
69
|
|
|
public function supportsClass($class) |
70
|
|
|
{ |
71
|
|
|
return $class === User::class; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $token |
76
|
|
|
* |
77
|
|
|
* @return UserInterface|null |
78
|
|
|
*/ |
79
|
|
|
public function findByPasswordResetToken($token) |
80
|
|
|
{ |
81
|
|
|
return $this->findByUsername('reset'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function getSupportedClass() |
88
|
|
|
{ |
89
|
|
|
return User::class; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function add(UserInterface $user) |
96
|
|
|
{ |
97
|
|
|
$this->users[] = $user; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function save(UserInterface $user) |
104
|
|
|
{ |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function update(UserInterface $user) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
foreach ($this->users as $key => $row) { |
113
|
|
|
if ($row->getId() === $user->getId()) { |
114
|
|
|
$this->users[$key] = $user; |
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function delete(UserInterface $user) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
foreach ($this->users as $key => $row) { |
126
|
|
|
if ($row->getUserName() === $user->getUserName()) { |
127
|
|
|
unset($this->users[$key]); |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.