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\Mutation\Organization; |
16
|
|
|
|
17
|
|
|
use Kreta\SharedKernel\Application\CommandBus; |
18
|
|
|
use Kreta\SharedKernel\Http\GraphQl\Relay\Mutation; |
19
|
|
|
use Kreta\TaskManager\Application\Command\Organization\RemoveOrganizationMemberToOrganizationCommand; |
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException; |
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedOrganizationActionException; |
22
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserDoesNotExistException; |
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
|
|
View Code Duplication |
class RemoveMemberToOrganizationMutation implements Mutation |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
private $commandBus; |
30
|
|
|
private $currentUser; |
31
|
|
|
private $organizationResolver; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
TokenStorageInterface $tokenStorage, |
35
|
|
|
CommandBus $commandBus, |
36
|
|
|
OrganizationResolver $organizationResolver |
37
|
|
|
) { |
38
|
|
|
$this->commandBus = $commandBus; |
39
|
|
|
$this->currentUser = $tokenStorage->getToken()->getUser()->getUsername(); |
40
|
|
|
$this->organizationResolver = $organizationResolver; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function execute(array $values) : array |
44
|
|
|
{ |
45
|
|
|
$command = new RemoveOrganizationMemberToOrganizationCommand( |
46
|
|
|
$values['userId'], |
47
|
|
|
$values['organizationId'], |
48
|
|
|
$this->currentUser |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$this->commandBus->handle($command); |
53
|
|
|
} catch (OrganizationDoesNotExistException $exception) { |
54
|
|
|
throw new UserError( |
55
|
|
|
sprintf( |
56
|
|
|
'The organization with "%s" id does not exist', |
57
|
|
|
$values['organizationId'] |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} catch (UnauthorizedOrganizationActionException $exception) { |
61
|
|
|
throw new UserError( |
62
|
|
|
sprintf( |
63
|
|
|
'The "%s" user does not allow to edit the organization', |
64
|
|
|
$this->currentUser |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} catch (UserDoesNotExistException $exception) { |
68
|
|
|
throw new UserError( |
69
|
|
|
sprintf( |
70
|
|
|
'The user with "%s" id does not exist', |
71
|
|
|
$values['userId'] |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$organization = $this->organizationResolver->resolve([ |
77
|
|
|
'id' => $values['organizationId'], |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
return [ |
81
|
|
|
'organization' => $organization, |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.