|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Donut Social Network - Yet another experimental social network. |
|
5
|
|
|
* Copyright (C) 2016-2018, Dejan Angelov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of Donut Social Network. |
|
8
|
|
|
* |
|
9
|
|
|
* Donut Social Network is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by |
|
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* Donut Social Network is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with Donut Social Network. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Donut Social Network |
|
23
|
|
|
* @copyright Copyright (C) 2016-2018, Dejan Angelov <[email protected]> |
|
24
|
|
|
* @license https://github.com/angelov/donut/blob/master/LICENSE |
|
25
|
|
|
* @author Dejan Angelov <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace Angelov\Donut\Users\Handlers; |
|
29
|
|
|
|
|
30
|
|
|
use Angelov\Donut\Core\EventBus\EventBusInterface; |
|
31
|
|
|
use Angelov\Donut\Core\Exceptions\ResourceNotFoundException; |
|
32
|
|
|
use Angelov\Donut\Users\Commands\StoreUserCommand; |
|
33
|
|
|
use Angelov\Donut\Users\EmailAvailabilityChecker\EmailAvailabilityCheckerInterface; |
|
34
|
|
|
use Angelov\Donut\Users\Events\UserRegisteredEvent; |
|
35
|
|
|
use Angelov\Donut\Users\Exceptions\EmailTakenException; |
|
36
|
|
|
use Angelov\Donut\Users\Repositories\UsersRepositoryInterface; |
|
37
|
|
|
use Angelov\Donut\Users\User; |
|
38
|
|
|
use Angelov\Donut\Places\Repositories\CitiesRepositoryInterface; |
|
39
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
40
|
|
|
|
|
41
|
|
|
class StoreUserCommandHandler |
|
42
|
|
|
{ |
|
43
|
|
|
private $users; |
|
44
|
|
|
private $passwordEncoder; |
|
45
|
|
|
private $emailAvailabilityChecker; |
|
46
|
|
|
private $eventBus; |
|
47
|
|
|
private $cities; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct( |
|
50
|
|
|
UsersRepositoryInterface $users, |
|
51
|
|
|
CitiesRepositoryInterface $cities, |
|
52
|
|
|
UserPasswordEncoderInterface $passwordEncoder, |
|
53
|
|
|
EmailAvailabilityCheckerInterface $emailAvailabilityChecker, |
|
54
|
|
|
EventBusInterface $eventBus |
|
55
|
|
|
) { |
|
56
|
|
|
$this->users = $users; |
|
57
|
|
|
$this->passwordEncoder = $passwordEncoder; |
|
58
|
|
|
$this->emailAvailabilityChecker = $emailAvailabilityChecker; |
|
59
|
|
|
$this->eventBus = $eventBus; |
|
60
|
|
|
$this->cities = $cities; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @throws EmailTakenException |
|
65
|
|
|
* @throws ResourceNotFoundException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function handle(StoreUserCommand $command) : void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->assertEmailNotTaken($command->getEmail()); |
|
70
|
|
|
|
|
71
|
|
|
$city = $this->cities->find($command->getCityId()); |
|
72
|
|
|
|
|
73
|
|
|
$user = new User( |
|
74
|
|
|
$command->getId(), |
|
75
|
|
|
$command->getName(), |
|
76
|
|
|
$command->getEmail(), |
|
77
|
|
|
$command->getPassword(), |
|
78
|
|
|
$city |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$password = $this->passwordEncoder->encodePassword($user, $command->getPassword()); |
|
82
|
|
|
|
|
83
|
|
|
$user->setPassword($password); |
|
84
|
|
|
|
|
85
|
|
|
$this->users->store($user); |
|
86
|
|
|
|
|
87
|
|
|
$this->eventBus->fire(new UserRegisteredEvent($user)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function assertEmailNotTaken(string $email) : void |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->emailAvailabilityChecker->isTaken($email)) { |
|
93
|
|
|
throw new EmailTakenException($email); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|