|
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\Project; |
|
16
|
|
|
|
|
17
|
|
|
use Kreta\SharedKernel\Domain\Model\Identity\Slug; |
|
18
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Organization; |
|
19
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException; |
|
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId; |
|
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Project; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectAlreadyExists; |
|
24
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectId; |
|
25
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectName; |
|
26
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository; |
|
27
|
|
|
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedCreateProjectException; |
|
28
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
|
29
|
|
|
|
|
30
|
|
|
class CreateProjectHandler |
|
31
|
|
|
{ |
|
32
|
|
|
private $organizationRepository; |
|
33
|
|
|
private $projectRepository; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
OrganizationRepository $organizationRepository, |
|
37
|
|
|
ProjectRepository $projectRepository |
|
38
|
|
|
) { |
|
39
|
|
|
$this->organizationRepository = $organizationRepository; |
|
40
|
|
|
$this->projectRepository = $projectRepository; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function __invoke(CreateProjectCommand $command) |
|
44
|
|
|
{ |
|
45
|
|
|
$project = $this->projectRepository->projectOfId( |
|
46
|
|
|
ProjectId::generate($command->id()) |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
if ($project instanceof Project) { |
|
50
|
|
|
throw new ProjectAlreadyExists($project->id()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$organizationId = OrganizationId::generate($command->organizationId()); |
|
54
|
|
|
$organization = $this->organizationRepository->organizationOfId($organizationId); |
|
55
|
|
|
|
|
56
|
|
|
if (!$organization instanceof Organization) { |
|
57
|
|
|
throw new OrganizationDoesNotExistException(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (!$organization->isOwner(UserId::generate($command->creatorId()))) { |
|
61
|
|
|
throw new UnauthorizedCreateProjectException(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$project = new Project( |
|
65
|
|
|
ProjectId::generate( |
|
66
|
|
|
$command->id() |
|
67
|
|
|
), |
|
68
|
|
|
new ProjectName( |
|
69
|
|
|
$command->name() |
|
70
|
|
|
), |
|
71
|
|
|
new Slug( |
|
72
|
|
|
null === $command->slug() ? $command->name() : $command->slug() |
|
73
|
|
|
), |
|
74
|
|
|
$organization->id() |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$this->projectRepository->persist($project); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|