|
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\Query\Project; |
|
16
|
|
|
|
|
17
|
|
|
use Kreta\SharedKernel\Domain\Model\Identity\Slug; |
|
18
|
|
|
use Kreta\TaskManager\Application\DataTransformer\Project\ProjectDataTransformer; |
|
19
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Organization; |
|
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
|
21
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Project; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository; |
|
24
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory; |
|
25
|
|
|
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectResourceException; |
|
26
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
|
27
|
|
|
|
|
28
|
|
|
class ProjectOfSlugHandler |
|
29
|
|
|
{ |
|
30
|
|
|
private $repository; |
|
31
|
|
|
private $specificationFactory; |
|
32
|
|
|
private $organizationRepository; |
|
33
|
|
|
private $dataTransformer; |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
36
|
|
|
OrganizationRepository $organizationRepository, |
|
37
|
|
|
ProjectRepository $repository, |
|
38
|
|
|
ProjectSpecificationFactory $specificationFactory, |
|
39
|
|
|
ProjectDataTransformer $dataTransformer |
|
40
|
|
|
) { |
|
41
|
|
|
$this->repository = $repository; |
|
42
|
|
|
$this->specificationFactory = $specificationFactory; |
|
43
|
|
|
$this->dataTransformer = $dataTransformer; |
|
44
|
|
|
$this->organizationRepository = $organizationRepository; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function __invoke(ProjectOfSlugQuery $query) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$projectSlug = new Slug($query->slug()); |
|
50
|
|
|
$organizationSlug = new Slug($query->organizationSlug()); |
|
51
|
|
|
$userId = UserId::generate($query->userId()); |
|
52
|
|
|
|
|
53
|
|
|
$project = $this->repository->singleResultQuery( |
|
54
|
|
|
$this->specificationFactory->buildBySlugSpecification( |
|
55
|
|
|
$projectSlug, |
|
56
|
|
|
$organizationSlug |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
|
|
$this->checkProjectExists($project); |
|
60
|
|
|
$organization = $this->organizationRepository->organizationOfSlug($organizationSlug); |
|
61
|
|
|
$this->checkUserIsOrganizationMember($organization, $userId); |
|
62
|
|
|
|
|
63
|
|
|
$this->dataTransformer->write($project); |
|
64
|
|
|
|
|
65
|
|
|
return $this->dataTransformer->read(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function checkProjectExists(Project $project = null) : void |
|
69
|
|
|
{ |
|
70
|
|
|
if (!$project instanceof Project) { |
|
71
|
|
|
throw new ProjectDoesNotExistException(); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function checkUserIsOrganizationMember(Organization $organization, UserId $userId) : void |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$organization->isOrganizationMember($userId)) { |
|
78
|
|
|
throw new UnauthorizedProjectResourceException(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
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.