1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenTribes\Core\UseCase; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use OpenTribes\Core\Repository\UserRepository; |
7
|
|
|
use OpenTribes\Core\Request\RegistrationRequest; |
8
|
|
|
use OpenTribes\Core\Response\RegistrationResponse; |
9
|
|
|
use OpenTribes\Core\Service\PasswordHashService; |
10
|
|
|
use OpenTribes\Core\Validator\RegistrationValidator; |
11
|
|
|
use Psr\Log\LoggerAwareInterface; |
12
|
|
|
use Psr\Log\LoggerAwareTrait; |
13
|
|
|
use Psr\Log\NullLogger; |
14
|
|
|
|
15
|
|
|
class RegistrationUseCase implements LoggerAwareInterface{ |
16
|
|
|
use LoggerAwareTrait; |
17
|
|
|
/** |
18
|
|
|
* @var UserRepository |
19
|
|
|
*/ |
20
|
|
|
private $userRepository; |
21
|
|
|
/** |
22
|
|
|
* @var RegistrationValidator |
23
|
|
|
*/ |
24
|
|
|
private $validator; |
25
|
|
|
/** |
26
|
|
|
* @var PasswordHashService |
27
|
|
|
*/ |
28
|
|
|
private $passwordHashService; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param UserRepository $userRepository |
32
|
|
|
* @param RegistrationValidator $validator |
33
|
|
|
* @param PasswordHashService $passwordHashService |
34
|
|
|
*/ |
35
|
37 |
|
public function __construct( |
36
|
|
|
UserRepository $userRepository, |
37
|
|
|
RegistrationValidator $validator, |
38
|
|
|
PasswordHashService $passwordHashService |
39
|
|
|
) { |
40
|
37 |
|
$this->userRepository = $userRepository; |
41
|
37 |
|
$this->validator = $validator; |
42
|
37 |
|
$this->passwordHashService = $passwordHashService; |
43
|
37 |
|
$this->logger = new NullLogger(); |
44
|
37 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param RegistrationRequest $request |
48
|
|
|
*/ |
49
|
36 |
|
private function setValidatorValues(RegistrationRequest $request) |
50
|
|
|
{ |
51
|
36 |
|
$this->validator->username = $request->getUsername(); |
52
|
36 |
|
$this->validator->password = $request->getPassword(); |
53
|
36 |
|
$this->validator->passwordConfirm = $request->getPasswordConfirm(); |
54
|
36 |
|
$this->validator->email = $request->getEmail(); |
55
|
36 |
|
$this->validator->emailConfirm = $request->getEmailConfirm(); |
56
|
36 |
|
$this->validator->acceptedTerms = $request->hasAcceptedTerms(); |
57
|
36 |
|
$this->validator->usernameExists = (bool)$this->userRepository->findByUsername($request->getUsername()); |
58
|
36 |
|
$this->validator->emailExists = (bool)$this->userRepository->findByEmail($request->getEmail()); |
59
|
36 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param RegistrationRequest $request |
63
|
|
|
* @param RegistrationResponse $response |
64
|
|
|
*/ |
65
|
36 |
|
public function process(RegistrationRequest $request, RegistrationResponse $response) |
66
|
|
|
{ |
67
|
36 |
|
$response->setRegistrationRequest($request); |
68
|
36 |
|
$this->setValidatorValues($request); |
69
|
|
|
|
70
|
36 |
|
if (!$this->validator->isValid()) { |
71
|
26 |
|
$response->setErrors($this->validator->getErrors()); |
72
|
26 |
|
return; |
73
|
|
|
} |
74
|
10 |
|
$this->createUser($request); |
75
|
10 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param RegistrationRequest $request |
79
|
|
|
*/ |
80
|
10 |
|
private function createUser(RegistrationRequest $request) |
81
|
|
|
{ |
82
|
10 |
|
$userId = $this->userRepository->getUniqueId(); |
83
|
10 |
|
$passwordHash = $this->passwordHashService->hash($request->getPassword()); |
84
|
10 |
|
$user = $this->userRepository->create($userId, $request->getUsername(), $passwordHash, $request->getEmail()); |
85
|
10 |
|
$user->setRegistrationDate(new DateTime()); |
86
|
10 |
|
$this->userRepository->add($user); |
87
|
|
|
} |
88
|
|
|
} |