Completed
Push — master ( 0bb8f9...15e42a )
by Beñat
06:24 queued 02:36
created

CreateOrganizationMutation::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 1
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
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\CreateOrganizationCommand;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationAlreadyExistsException;
21
use Kreta\TaskManager\Infrastructure\Symfony\GraphQl\Query\Organization\OrganizationResolver;
22
use Overblog\GraphQLBundle\Error\UserError;
23
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24
25
class CreateOrganizationMutation implements Mutation
26
{
27
    private $commandBus;
28
    private $currentUser;
29
    private $organizationResolver;
30
31 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
32
        TokenStorageInterface $tokenStorage,
33
        CommandBus $commandBus,
34
        OrganizationResolver $organizationResolver
35
    ) {
36
        $this->commandBus = $commandBus;
37
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
38
        $this->organizationResolver = $organizationResolver;
39
    }
40
41
    public function execute(array $values) : array
42
    {
43
        $command = new CreateOrganizationCommand(
44
            $this->currentUser,
45
            $values['name']
46
        );
47
48
        try {
49
            $this->commandBus->handle($command);
50
        } catch (OrganizationAlreadyExistsException $exception) {
51
            throw new UserError(
52
                sprintf(
53
                    'The organization with "%s" name already exists',
54
                    $values['name']
55
                )
56
            );
57
        }
58
59
        $organization = $this->organizationResolver->resolve([
60
            'id' => $command->id(),
61
        ]);
62
63
        return [
64
            'organization' => $organization,
65
        ];
66
    }
67
}
68