Completed
Pull Request — master (#227)
by Beñat
03:56
created

MemberResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B resolve() 0 31 3
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\Ui\Http\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\OwnerDoesNotExistException;
22
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
23
24
class MemberResolver implements Resolver
25
{
26
    private $queryBus;
27
    private $currentUser;
28
    private $organizationResolver;
29
30
    public function __construct(
31
        TokenStorageInterface $tokenStorage,
32
        QueryBus $queryBus,
33
        OrganizationResolver $organizationResolver
34
    ) {
35
        $this->queryBus = $queryBus;
36
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
37
        $this->organizationResolver = $organizationResolver;
38
    }
39
40
    public function resolve($args)
41
    {
42
        try {
43
            $this->queryBus->handle(
44
                new OwnerOfIdQuery(
45
                    $args['organizationId'],
46
                    $args['ownerId'],
47
                    $this->currentUser
48
                ),
49
                $result
50
            );
51
        } catch (OwnerDoesNotExistException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
52
        }
53
54
        if (empty($result)) {
55
            $this->queryBus->handle(
56
                new OrganizationMemberOfIdQuery(
57
                    $args['organizationId'],
58
                    $args['organizationMemberId'],
59
                    $this->currentUser
60
                ),
61
                $result
62
            );
63
        }
64
65
        $result['organization'] = $this->organizationResolver->resolve([
66
            'id' => $args['organizationId'],
67
        ]);
68
69
        return $result;
70
    }
71
}
72