| 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\ProjectSpecificationFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Kreta\TaskManager\Domain\Model\Project\UnauthorizedCreateProjectException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Kreta\TaskManager\Domain\Model\User\UserId; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | class CreateProjectHandler | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     private $organizationRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     private $repository; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     private $specificationFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         OrganizationRepository $organizationRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ProjectRepository $repository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ProjectSpecificationFactory $specificationFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->organizationRepository = $organizationRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->repository = $repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->specificationFactory = $specificationFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function __invoke(CreateProjectCommand $command) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $name = $command->name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $slug = $command->slug(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $slug = new Slug(null === $slug ? $name : $slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $name = new ProjectName($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $projectId = ProjectId::generate($command->id()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $organizationId = OrganizationId::generate($command->organizationId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $creatorId = UserId::generate($command->creatorId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $organization = $this->organizationRepository->organizationOfId($organizationId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->checkOrganizationExists($organization); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->checkProjectUniqueness($projectId, $slug, $organization->slug()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->checkCreatorPrivileges($organization, $creatorId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $project = new Project($projectId, $name, $slug, $organizationId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->repository->persist($project); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     private function checkOrganizationExists(?Organization $organization) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (!$organization instanceof Organization) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             throw new OrganizationDoesNotExistException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     private function checkProjectUniqueness(ProjectId $projectId, Slug $slug, Slug $organizationSlug) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->checkProjectIdUniqueness($projectId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->checkProjectSlugUniqueness($slug, $organizationSlug); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     private function checkProjectIdUniqueness(ProjectId $projectId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $project = $this->repository->projectOfId($projectId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if ($project instanceof Project) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             throw ProjectAlreadyExists::fromId($projectId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     private function checkProjectSlugUniqueness(Slug $slug, Slug $organizationSlug) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $project = $this->repository->singleResultQuery( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $this->specificationFactory->buildBySlugSpecification( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 $slug, $organizationSlug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if ($project instanceof Project) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             throw ProjectAlreadyExists::fromSlugs($slug, $organizationSlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     private function checkCreatorPrivileges(Organization $organization, UserId $creatorId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (!$organization->isOwner($creatorId)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             throw new UnauthorizedCreateProjectException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |