|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sinergi\Users\User; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Sinergi\Users\Container; |
|
7
|
|
|
use Sinergi\Users\User\Exception\InvalidUserException; |
|
8
|
|
|
|
|
9
|
|
|
class UserController |
|
10
|
|
|
{ |
|
11
|
|
|
private $container; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(ContainerInterface $container) |
|
14
|
|
|
{ |
|
15
|
|
|
if ($container instanceof Container) { |
|
16
|
|
|
$this->container = $container; |
|
17
|
|
|
} else { |
|
18
|
|
|
$this->container = new Container($container); |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function createUser(UserEntityInterface $user): UserEntityInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var UserValidatorInterface $userValidator */ |
|
25
|
|
|
$userValidator = $this->container->get(UserValidatorInterface::class); |
|
26
|
|
|
$errors = $userValidator($user); |
|
27
|
|
|
|
|
28
|
|
|
if (!empty($errors)) { |
|
29
|
|
|
throw new InvalidUserException($errors); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
|
33
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
|
34
|
|
|
$userRepository->save($user); |
|
35
|
|
|
|
|
36
|
|
|
$this->getEmailConfirmationController() |
|
|
|
|
|
|
37
|
|
|
->sendConfirmationEmail($user); |
|
38
|
|
|
|
|
39
|
|
|
return $user; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param UserEntity $userEntity |
|
44
|
|
|
* @param array $parameters |
|
45
|
|
|
*/ |
|
46
|
|
|
public function updateUser(UserEntity $userEntity, array $parameters) |
|
47
|
|
|
{ |
|
48
|
|
|
$options = [UserValidator::OPTION_SKIP_PASSWORD_VALIDATION]; |
|
49
|
|
|
|
|
50
|
|
|
//todo : refactored this : |
|
51
|
|
|
|
|
52
|
|
|
$parameters = array_merge($userEntity->toArray(), $parameters); |
|
53
|
|
|
|
|
54
|
|
|
if ($userEntity->getEmail() === $parameters['email']) { |
|
55
|
|
|
$options[] = UserValidator::OPTION_SKIP_EMAIL_UNIQUENESS; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->getUserValidator()->validateParameters($parameters, $options); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
if ($userEntity->getEmail() !== $parameters['email']) { |
|
61
|
|
|
$userEntity->setPendingEmail($parameters['email']); |
|
62
|
|
|
|
|
63
|
|
|
(new EmailConfirmationController($this->getContainer()))->emailUpdated($userEntity); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$userEntity |
|
67
|
|
|
->setName($parameters['name']) |
|
68
|
|
|
->setPhone($parameters['phone']); |
|
69
|
|
|
|
|
70
|
|
|
$this->getEntityManager()->persist($userEntity); |
|
|
|
|
|
|
71
|
|
|
$this->getEntityManager()->flush($userEntity); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param userEntity $userEntity |
|
76
|
|
|
* @param array $parameters |
|
77
|
|
|
* @throws AuthenticationException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function updatePassword(userEntity $userEntity, array $parameters) |
|
80
|
|
|
{ |
|
81
|
|
|
if (!$userEntity->testPassword($parameters['current-password'])) { |
|
82
|
|
|
throw new AuthenticationException($this->getDictionary()->get('user.password.error.wrong_password')); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$this->getUserValidator()->validateParameters($parameters, |
|
|
|
|
|
|
86
|
|
|
[UserValidator::OPTION_JUST_PASSWORD]); |
|
87
|
|
|
|
|
88
|
|
|
$userEntity->setPassword($parameters['password']); |
|
89
|
|
|
$this->getEntityManager()->persist($userEntity); |
|
|
|
|
|
|
90
|
|
|
$this->getEntityManager()->flush($userEntity); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param userEntity $userEntity |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
public function deleteUser(userEntity $userEntity) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$userEntity->setStatus(UserEntity::STATUS_DELETED); |
|
99
|
|
|
$userEntity->setDeletedEmail($userEntity->getEmail()); |
|
100
|
|
|
$userEntity->setEmail(null); |
|
101
|
|
|
$this->getContainer()->getEntityManager()->persist($userEntity); |
|
|
|
|
|
|
102
|
|
|
$this->getContainer()->getEntityManager()->flush($userEntity); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param userEntity $userEntity |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
public function banUser(userEntity $userEntity) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$userEntity->setStatus(UserEntity::STATUS_BANNED); |
|
111
|
|
|
$userEntity->setDeletedEmail($userEntity->getEmail()); |
|
112
|
|
|
$userEntity->setEmail(null); |
|
113
|
|
|
$this->getContainer()->getEntityManager()->persist($userEntity); |
|
|
|
|
|
|
114
|
|
|
$this->getContainer()->getEntityManager()->flush($userEntity); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $status |
|
119
|
|
|
* |
|
120
|
|
|
* @return mixed |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getUserStatusLabel($status) |
|
123
|
|
|
{ |
|
124
|
|
|
switch ($status) { |
|
125
|
|
|
case UserEntity::STATUS_ACTIVE: |
|
126
|
|
|
return $this->getDictionary()->get('user.status.active'); |
|
|
|
|
|
|
127
|
|
|
case UserEntity::STATUS_DELETED: |
|
128
|
|
|
return $this->getDictionary()->get('user.status.deleted'); |
|
|
|
|
|
|
129
|
|
|
break; |
|
|
|
|
|
|
130
|
|
|
case UserEntity::STATUS_BANNED: |
|
131
|
|
|
return $this->getDictionary()->get('user.status.banned'); |
|
|
|
|
|
|
132
|
|
|
break; |
|
|
|
|
|
|
133
|
|
|
default: |
|
134
|
|
|
return $this->getDictionary()->get('user.status.inactive'); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.