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) |
|
|
|
|
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
|
|
|
|
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.