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\OwnerOfIdQuery; |
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException; |
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OwnerDoesNotExistException; |
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
|
|
View Code Duplication |
class OwnerResolver 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
|
|
|
try { |
40
|
|
|
$this->queryBus->handle( |
41
|
|
|
new OwnerOfIdQuery( |
42
|
|
|
$args['organizationId'], |
43
|
|
|
$args['ownerId'], |
44
|
|
|
$this->currentUser |
45
|
|
|
), |
46
|
|
|
$result |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
return $result; |
50
|
|
|
} catch (OrganizationDoesNotExistException $exception) { |
51
|
|
|
throw new UserError( |
52
|
|
|
sprintf( |
53
|
|
|
'Does not exist any organization with the given "%s" id', |
54
|
|
|
$args['organizationId'] |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} catch (UnauthorizedOrganizationActionException $exception) { |
58
|
|
|
throw new UserError( |
59
|
|
|
sprintf( |
60
|
|
|
'The "%s" user does not allow to access the "%s" organization', |
61
|
|
|
$this->currentUser, |
62
|
|
|
$args['organizationId'] |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} catch (OwnerDoesNotExistException $exception) { |
66
|
|
|
throw new UserError( |
67
|
|
|
sprintf( |
68
|
|
|
'Does not exist any owner with the given "%s" id', |
69
|
|
|
$args['ownerId'] |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.