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\EditOrganizationCommand; |
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationAlreadyExistsException; |
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException; |
22
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedEditOrganizationException; |
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 EditOrganizationMutation 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 EditOrganizationCommand( |
46
|
|
|
$values['id'], |
47
|
|
|
$values['name'], |
48
|
|
|
$this->currentUser |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$this->commandBus->handle($command); |
53
|
|
|
} catch (OrganizationAlreadyExistsException $exception) { |
54
|
|
|
throw new UserError( |
55
|
|
|
sprintf( |
56
|
|
|
'The organization with "%s" name already exists', |
57
|
|
|
$values['name'] |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} catch (OrganizationDoesNotExistException $exception) { |
61
|
|
|
throw new UserError( |
62
|
|
|
sprintf( |
63
|
|
|
'The organization with "%s" id does not exist', |
64
|
|
|
$values['id'] |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} catch (UnauthorizedEditOrganizationException $exception) { |
68
|
|
|
throw new UserError( |
69
|
|
|
sprintf( |
70
|
|
|
'The "%s" user does not allow to edit the organization', |
71
|
|
|
$this->currentUser |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$organization = $this->organizationResolver->resolve([ |
77
|
|
|
'id' => $command->id(), |
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.