|
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\SharedKernel\Domain\Model\Identity\Slug; |
|
18
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Organization; |
|
19
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationAlreadyExistsException; |
|
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId; |
|
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationName; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\User\User; |
|
24
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserDoesNotExistException; |
|
25
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
|
26
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserRepository; |
|
27
|
|
|
|
|
28
|
|
|
class CreateOrganizationHandler |
|
29
|
|
|
{ |
|
30
|
|
|
private $repository; |
|
31
|
|
|
private $userRepository; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(OrganizationRepository $repository, UserRepository $userRepository) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->repository = $repository; |
|
36
|
|
|
$this->userRepository = $userRepository; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function __invoke(CreateOrganizationCommand $command) |
|
40
|
|
|
{ |
|
41
|
|
|
$name = $command->name(); |
|
42
|
|
|
$slug = $command->slug(); |
|
43
|
|
|
$slug = new Slug(null === $slug ? $name : $slug); |
|
44
|
|
|
$name = new OrganizationName($name); |
|
45
|
|
|
$organizationId = OrganizationId::generate($command->id()); |
|
46
|
|
|
$reporterId = UserId::generate($command->reporterId()); |
|
47
|
|
|
|
|
48
|
|
|
$this->checkOrganizationUniqueness($organizationId, $slug); |
|
49
|
|
|
$this->checkReporterExists($reporterId); |
|
50
|
|
|
$organization = new Organization($organizationId, $name, $slug, $reporterId); |
|
51
|
|
|
$this->repository->persist($organization); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function checkOrganizationUniqueness(OrganizationId $organizationId, Slug $slug) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->checkOrganizationIdUniqueness($organizationId); |
|
57
|
|
|
$this->checkOrganizationSlugUniqueness($slug); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function checkOrganizationIdUniqueness(OrganizationId $organizationId) |
|
61
|
|
|
{ |
|
62
|
|
|
$organization = $this->repository->organizationOfId($organizationId); |
|
63
|
|
|
if ($organization instanceof Organization) { |
|
64
|
|
|
throw new OrganizationAlreadyExistsException(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function checkOrganizationSlugUniqueness(Slug $slug) |
|
69
|
|
|
{ |
|
70
|
|
|
$organization = $this->repository->organizationOfSlug($slug); |
|
71
|
|
|
if ($organization instanceof Organization) { |
|
72
|
|
|
throw new OrganizationAlreadyExistsException(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function checkReporterExists(UserId $reporterId) |
|
77
|
|
|
{ |
|
78
|
|
|
$reporter = $this->userRepository->userOfId($reporterId); |
|
79
|
|
|
if (!$reporter instanceof User) { |
|
80
|
|
|
throw new UserDoesNotExistException(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|