Completed
Push — master ( e94dcc...c29e00 )
by Gorka
12s
created

OrganizationResolver::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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\OrganizationOfIdQuery;
20
use Kreta\TaskManager\Application\Query\Organization\OrganizationOfSlugQuery;
21
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException;
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
class OrganizationResolver 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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
38
    {
39
        if (isset($args['id'])) {
40
            return $this->byId($args['id']);
41
        }
42
43
        return $this->bySlug($args['slug']);
44
    }
45
46 View Code Duplication
    private function byId($id)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
47
    {
48
        try {
49
            $this->queryBus->handle(
50
                new OrganizationOfIdQuery(
51
                    $id,
52
                    $this->currentUser
53
                ),
54
                $result
55
            );
56
57
            return $result;
58
        } catch (OrganizationDoesNotExistException $exception) {
59
            throw new UserError(
60
                sprintf(
61
                    'Does no exist any organization with the given "%s" id',
62
                    $id
63
                )
64
            );
65
        } catch (UnauthorizedOrganizationActionException $exception) {
66
            throw new UserError(
67
                sprintf(
68
                    'The "%s" user does not allow to access the "%s" organization',
69
                    $this->currentUser,
70
                    $id
71
                )
72
            );
73
        }
74
    }
75
76 View Code Duplication
    public function bySlug($slug)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
77
    {
78
        try {
79
            $this->queryBus->handle(
80
                new OrganizationOfSlugQuery(
81
                    $slug,
82
                    $this->currentUser
83
                ),
84
                $result
85
            );
86
87
            return $result;
88
        } catch (OrganizationDoesNotExistException $exception) {
89
            throw new UserError(
90
                sprintf(
91
                    'Does no exist any organization with the given "%s" slug',
92
                    $slug
93
                )
94
            );
95
        } catch (UnauthorizedOrganizationActionException $exception) {
96
            throw new UserError(
97
                sprintf(
98
                    'The "%s" user does not allow to access the "%s" organization',
99
                    $this->currentUser,
100
                    $slug
101
                )
102
            );
103
        }
104
    }
105
}
106