Completed
Push — master ( ccb856...23db26 )
by Beñat
11s
created

MemberResolver::owner()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 37
Code Lines 25

Duplication

Lines 37
Ratio 100 %

Importance

Changes 0
Metric Value
dl 37
loc 37
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 25
nc 4
nop 2
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\OrganizationMemberOfIdQuery;
20
use Kreta\TaskManager\Application\Query\Organization\OwnerOfIdQuery;
21
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException;
22
use Kreta\TaskManager\Domain\Model\Organization\OrganizationMemberDoesNotExistException;
23
use Kreta\TaskManager\Domain\Model\Organization\OwnerDoesNotExistException;
24
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedOrganizationActionException;
25
use Overblog\GraphQLBundle\Error\UserError;
26
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
27
28
class MemberResolver implements Resolver
29
{
30
    private $queryBus;
31
    private $currentUser;
32
    private $organizationResolver;
33
34 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...
35
        TokenStorageInterface $tokenStorage,
36
        QueryBus $queryBus,
37
        OrganizationResolver $organizationResolver
38
    ) {
39
        $this->queryBus = $queryBus;
40
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
41
        $this->organizationResolver = $organizationResolver;
42
    }
43
44
    public function resolve($args)
45
    {
46
        if (isset($args['ownerId'])) {
47
            $result = $this->owner(
48
                $args['organizationId'],
49
                $args['ownerId']
50
            );
51
        } else {
52
            $result = $this->organizationMember(
53
                $args['organizationId'],
54
                $args['organizationMemberId']
55
            );
56
        }
57
        $result['organization'] = $this->organizationResolver->resolve([
58
            'id' => $args['organizationId'],
59
        ]);
60
61
        return $result;
62
    }
63
64 View Code Duplication
    private function owner($organizationId, $ownerId)
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...
65
    {
66
        try {
67
            $this->queryBus->handle(
68
                new OwnerOfIdQuery(
69
                    $organizationId,
70
                    $ownerId,
71
                    $this->currentUser
72
                ),
73
                $result
74
            );
75
76
            return $result;
77
        } catch (OrganizationDoesNotExistException $exception) {
78
            throw new UserError(
79
                sprintf(
80
                    'Does no exist any organization with the given "%s" id',
81
                    $organizationId
82
                )
83
            );
84
        } catch (UnauthorizedOrganizationActionException $exception) {
85
            throw new UserError(
86
                sprintf(
87
                    'The "%s" user does not allow to access the "%s" organization',
88
                    $this->currentUser,
89
                    $organizationId
90
                )
91
            );
92
        } catch (OwnerDoesNotExistException $exception) {
93
            throw new UserError(
94
                sprintf(
95
                    'Does no exist any owner with the given "%s" id',
96
                    $ownerId
97
                )
98
            );
99
        }
100
    }
101
102 View Code Duplication
    private function organizationMember($organizationId, $organizationMemberId)
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...
103
    {
104
        try {
105
            $this->queryBus->handle(
106
                new OrganizationMemberOfIdQuery(
107
                    $organizationId,
108
                    $organizationMemberId,
109
                    $this->currentUser
110
                ),
111
                $result
112
            );
113
114
            return $result;
115
        } catch (OrganizationDoesNotExistException $exception) {
116
            throw new UserError(
117
                sprintf(
118
                    'Does no exist any organization with the given "%s" id',
119
                    $organizationId
120
                )
121
            );
122
        } catch (UnauthorizedOrganizationActionException $exception) {
123
            throw new UserError(
124
                sprintf(
125
                    'The "%s" user does not allow to access the "%s" organization',
126
                    $this->currentUser,
127
                    $organizationId
128
                )
129
            );
130
        } catch (OrganizationMemberDoesNotExistException $exception) {
131
            throw new UserError(
132
                sprintf(
133
                    'Does no exist any organization member with the given "%s" id',
134
                    $organizationMemberId
135
                )
136
            );
137
        }
138
    }
139
}
140