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\Organization; |
16
|
|
|
|
17
|
|
|
use Kreta\SharedKernel\Application\QueryBus; |
18
|
|
|
use Kreta\SharedKernel\Http\GraphQl\Resolver; |
19
|
|
|
use Kreta\TaskManager\Application\Query\Organization\OrganizationOfIdQuery; |
20
|
|
|
use Kreta\TaskManager\Application\Query\Organization\OrganizationOfSlugQuery; |
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException; |
22
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedOrganizationActionException; |
23
|
|
|
use Overblog\GraphQLBundle\Error\UserError; |
24
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
25
|
|
|
|
26
|
|
|
class OrganizationResolver implements Resolver |
27
|
|
|
{ |
28
|
|
|
private $queryBus; |
29
|
|
|
private $currentUser; |
30
|
|
|
|
31
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, QueryBus $queryBus) |
32
|
|
|
{ |
33
|
|
|
$this->queryBus = $queryBus; |
34
|
|
|
$this->currentUser = $tokenStorage->getToken()->getUser()->getUsername(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function resolve($args) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if (isset($args['id'])) { |
40
|
|
|
return $this->byId($args['id']); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->bySlug($args['slug']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
private function byId($id) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
$this->queryBus->handle( |
50
|
|
|
new OrganizationOfIdQuery( |
51
|
|
|
$id, |
52
|
|
|
$this->currentUser |
53
|
|
|
), |
54
|
|
|
$result |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
return $result; |
58
|
|
|
} catch (OrganizationDoesNotExistException $exception) { |
59
|
|
|
throw new UserError( |
60
|
|
|
sprintf( |
61
|
|
|
'Does no exist any organization with the given "%s" id', |
62
|
|
|
$id |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} catch (UnauthorizedOrganizationActionException $exception) { |
66
|
|
|
throw new UserError( |
67
|
|
|
sprintf( |
68
|
|
|
'The "%s" user does not allow to access the "%s" organization', |
69
|
|
|
$this->currentUser, |
70
|
|
|
$id |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function bySlug($slug) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
$this->queryBus->handle( |
80
|
|
|
new OrganizationOfSlugQuery( |
81
|
|
|
$slug, |
82
|
|
|
$this->currentUser |
83
|
|
|
), |
84
|
|
|
$result |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return $result; |
88
|
|
|
} catch (OrganizationDoesNotExistException $exception) { |
89
|
|
|
throw new UserError( |
90
|
|
|
sprintf( |
91
|
|
|
'Does no exist any organization with the given "%s" slug', |
92
|
|
|
$slug |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} catch (UnauthorizedOrganizationActionException $exception) { |
96
|
|
|
throw new UserError( |
97
|
|
|
sprintf( |
98
|
|
|
'The "%s" user does not allow to access the "%s" organization', |
99
|
|
|
$this->currentUser, |
100
|
|
|
$slug |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.