Completed
Pull Request — master (#374)
by Beñat
04:28
created

AddMemberToOrganizationMutation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
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\AddOrganizationMemberToOrganizationCommand;
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 AddMemberToOrganizationMutation implements Mutation
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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 AddOrganizationMemberToOrganizationCommand(
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