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\Infrastructure\Symfony\GraphQl\Query\Project; |
16
|
|
|
|
17
|
|
|
use Kreta\SharedKernel\Application\QueryBus; |
18
|
|
|
use Kreta\SharedKernel\Http\GraphQl\Resolver; |
19
|
|
|
use Kreta\TaskManager\Application\Query\Project\ProjectOfIdQuery; |
20
|
|
|
use Kreta\TaskManager\Application\Query\Project\ProjectOfSlugQuery; |
21
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException; |
22
|
|
|
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectResourceException; |
23
|
|
|
use Kreta\TaskManager\Infrastructure\Symfony\GraphQl\Query\Organization\OrganizationResolver; |
24
|
|
|
use Overblog\GraphQLBundle\Error\UserError; |
25
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
26
|
|
|
|
27
|
|
|
class ProjectResolver implements Resolver |
28
|
|
|
{ |
29
|
|
|
private $queryBus; |
30
|
|
|
private $currentUser; |
31
|
|
|
private $organizationResolver; |
32
|
|
|
|
33
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
34
|
|
|
TokenStorageInterface $tokenStorage, |
35
|
|
|
QueryBus $queryBus, |
36
|
|
|
OrganizationResolver $organizationResolver |
37
|
|
|
) { |
38
|
|
|
$this->queryBus = $queryBus; |
39
|
|
|
$this->currentUser = $tokenStorage->getToken()->getUser()->getUsername(); |
40
|
|
|
$this->organizationResolver = $organizationResolver; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function resolve($args) |
44
|
|
|
{ |
45
|
|
|
$result = isset($args['projectInput']) |
46
|
|
|
? $this->byProjectInput($args['projectInput']) |
47
|
|
|
: $this->byId($args['id']); |
48
|
|
|
|
49
|
|
|
$result['organization'] = $this->organizationResolver->resolve([ |
50
|
|
|
'id' => $result['organization_id'], |
51
|
|
|
]); |
52
|
|
|
unset($result['organization_id']); |
53
|
|
|
|
54
|
|
|
return $result; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function byId($id) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$this->queryBus->handle( |
61
|
|
|
new ProjectOfIdQuery( |
62
|
|
|
$id, |
63
|
|
|
$this->currentUser |
64
|
|
|
), |
65
|
|
|
$result |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return $result; |
69
|
|
|
} catch (ProjectDoesNotExistException $exception) { |
70
|
|
|
throw new UserError( |
71
|
|
|
sprintf( |
72
|
|
|
'Does not exist any project with the given "%s" id', |
73
|
|
|
$id |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} catch (UnauthorizedProjectResourceException $exception) { |
77
|
|
|
throw new UserError( |
78
|
|
|
sprintf( |
79
|
|
|
'The "%s" user does not allow to access the "%s" project', |
80
|
|
|
$this->currentUser, |
81
|
|
|
$id |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function byProjectInput($projectInput) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
try { |
90
|
|
|
$this->queryBus->handle( |
91
|
|
|
new ProjectOfSlugQuery( |
92
|
|
|
$projectInput['slug'], |
93
|
|
|
$projectInput['organizationSlug'], |
94
|
|
|
$this->currentUser |
95
|
|
|
), |
96
|
|
|
$result |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return $result; |
100
|
|
|
} catch (ProjectDoesNotExistException $exception) { |
101
|
|
|
throw new UserError( |
102
|
|
|
sprintf( |
103
|
|
|
'Does not exist any project with the given "%s" slug', |
104
|
|
|
$projectInput['slug'] |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
} catch (UnauthorizedProjectResourceException $exception) { |
108
|
|
|
throw new UserError( |
109
|
|
|
sprintf( |
110
|
|
|
'The "%s" user does not allow to access the "%s" project', |
111
|
|
|
$this->currentUser, |
112
|
|
|
$projectInput['slug'] |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.