Completed
Push — master ( 44b3f2...31d5aa )
by Beñat
15s
created

TaskResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 58.49 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 31
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B resolve() 31 31 3
A resolveByParent() 0 4 2

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\Task;
16
17
use Kreta\SharedKernel\Application\QueryBus;
18
use Kreta\SharedKernel\Http\GraphQl\Resolver;
19
use Kreta\TaskManager\Application\Query\Project\Task\TaskOfIdQuery;
20
use Kreta\TaskManager\Domain\Model\Project\Task\TaskDoesNotExistException;
21
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskResourceException;
22
use Overblog\GraphQLBundle\Error\UserError;
23
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24
25
class TaskResolver implements Resolver
26
{
27
    private $queryBus;
28
    private $currentUser;
29
    private $taskBuilderResolver;
30
31
    public function __construct(
32
        TokenStorageInterface $tokenStorage,
33
        QueryBus $queryBus,
34
        TaskBuilderResolver $taskBuilderResolver
35
    ) {
36
        $this->queryBus = $queryBus;
37
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
38
        $this->taskBuilderResolver = $taskBuilderResolver;
39
    }
40
41 View Code Duplication
    public function resolve($args)
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...
42
    {
43
        try {
44
            $this->queryBus->handle(
45
                new TaskOfIdQuery(
46
                    $args['id'],
47
                    $this->currentUser
48
                ),
49
                $result
50
            );
51
52
            $result = $this->taskBuilderResolver->resolve($result);
53
        } catch (TaskDoesNotExistException $exception) {
54
            throw new UserError(
55
                sprintf(
56
                    'Does not exist any task with the given "%s" id',
57
                    $args['id']
58
                )
59
            );
60
        } catch (UnauthorizedTaskResourceException $exception) {
61
            throw new UserError(
62
                sprintf(
63
                    'The "%s" user does not allow to access the "%s" task',
64
                    $this->currentUser,
65
                    $args['id']
66
                )
67
            );
68
        }
69
70
        return $result;
71
    }
72
73
    public function resolveByParent($parentId)
74
    {
75
        return null === $parentId ? null : $this->resolve(['id' => $parentId]);
76
    }
77
}
78