1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Kreta 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
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Kreta\TaskManager\Application\Command\Organization; |
16
|
|
|
|
17
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Organization; |
18
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException; |
19
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId; |
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedOrganizationActionException; |
22
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserDoesNotExistException; |
23
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
24
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserRepository; |
25
|
|
|
|
26
|
|
View Code Duplication |
class AddOrganizationMemberToOrganizationHandler |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
private $repository; |
29
|
|
|
private $userRepository; |
30
|
|
|
|
31
|
|
|
public function __construct(OrganizationRepository $repository, UserRepository $userRepository) |
32
|
|
|
{ |
33
|
|
|
$this->repository = $repository; |
34
|
|
|
$this->userRepository = $userRepository; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function __invoke(AddOrganizationMemberToOrganizationCommand $command) |
38
|
|
|
{ |
39
|
|
|
$adderId = UserId::generate($command->adderId()); |
40
|
|
|
$userId = UserId::generate($command->userId()); |
41
|
|
|
$organizationId = OrganizationId::generate($command->organizationId()); |
42
|
|
|
|
43
|
|
|
$organization = $this->repository->organizationOfId($organizationId); |
44
|
|
|
$this->checkOrganizationExists($organization); |
45
|
|
|
$this->checkAdderIsOrganizationOwner($organization, $adderId); |
46
|
|
|
$this->checkUserExists($userId); |
47
|
|
|
$organization->addOrganizationMember($userId); |
48
|
|
|
$this->repository->persist($organization); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function checkOrganizationExists(Organization $organization = null) |
52
|
|
|
{ |
53
|
|
|
if (!$organization instanceof Organization) { |
54
|
|
|
throw new OrganizationDoesNotExistException(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function checkAdderIsOrganizationOwner(Organization $organization, UserId $adderId) |
59
|
|
|
{ |
60
|
|
|
if (!$organization->isOwner($adderId)) { |
61
|
|
|
throw new UnauthorizedOrganizationActionException(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function checkUserExists(UserId $userId) |
66
|
|
|
{ |
67
|
|
|
$user = $this->userRepository->userOfId($userId); |
68
|
|
|
if (null === $user) { |
69
|
|
|
throw new UserDoesNotExistException(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
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.