Completed
Pull Request — master (#371)
by Beñat
15:53
created

ProjectResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 9.89 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 9
loc 91
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A resolve() 0 13 2
B byId() 0 29 3
B byProjectInput() 0 30 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Project;
16
17
use Kreta\SharedKernel\Application\QueryBus;
18
use Kreta\SharedKernel\Http\GraphQl\Resolver;
19
use Kreta\TaskManager\Application\Query\Project\ProjectOfIdQuery;
20
use Kreta\TaskManager\Application\Query\Project\ProjectOfSlugQuery;
21
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException;
22
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectResourceException;
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
class ProjectResolver implements Resolver
28
{
29
    private $queryBus;
30
    private $currentUser;
31
    private $organizationResolver;
32
33 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...
34
        TokenStorageInterface $tokenStorage,
35
        QueryBus $queryBus,
36
        OrganizationResolver $organizationResolver
37
    ) {
38
        $this->queryBus = $queryBus;
39
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
40
        $this->organizationResolver = $organizationResolver;
41
    }
42
43
    public function resolve($args)
44
    {
45
        $result = isset($args['projectInput'])
46
            ? $this->byProjectInput($args['projectInput'])
47
            : $this->byId($args['id']);
48
49
        $result['organization'] = $this->organizationResolver->resolve([
50
            'id' => $result['organization_id'],
51
        ]);
52
        unset($result['organization_id']);
53
54
        return $result;
55
    }
56
57
    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...
58
    {
59
        try {
60
            $this->queryBus->handle(
61
                new ProjectOfIdQuery(
62
                    $id,
63
                    $this->currentUser
64
                ),
65
                $result
66
            );
67
68
            return $result;
69
        } catch (ProjectDoesNotExistException $exception) {
70
            throw new UserError(
71
                sprintf(
72
                    'Does not exist any project with the given "%s" id',
73
                    $id
74
                )
75
            );
76
        } catch (UnauthorizedProjectResourceException $exception) {
77
            throw new UserError(
78
                sprintf(
79
                    'The "%s" user does not allow to access the "%s" project',
80
                    $this->currentUser,
81
                    $id
82
                )
83
            );
84
        }
85
    }
86
87
    public function byProjectInput($projectInput)
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...
88
    {
89
        try {
90
            $this->queryBus->handle(
91
                new ProjectOfSlugQuery(
92
                    $projectInput['slug'],
93
                    $projectInput['organizationSlug'],
94
                    $this->currentUser
95
                ),
96
                $result
97
            );
98
99
            return $result;
100
        } catch (ProjectDoesNotExistException $exception) {
101
            throw new UserError(
102
                sprintf(
103
                    'Does not exist any project with the given "%s" slug',
104
                    $projectInput['slug']
105
                )
106
            );
107
        } catch (UnauthorizedProjectResourceException $exception) {
108
            throw new UserError(
109
                sprintf(
110
                    'The "%s" user does not allow to access the "%s" project',
111
                    $this->currentUser,
112
                    $projectInput['slug']
113
                )
114
            );
115
        }
116
    }
117
}
118