Test Setup Failed
Push — graphql_api ( 4ff753 )
by Herberto
07:48 queued 01:35
created

QueryResolverMap::map()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Presentation\Api\GraphQl;
16
17
use Acme\App\Core\Component\Blog\Application\Repository\PostRepositoryInterface;
18
use Acme\App\Core\Component\Blog\Domain\Post\Post;
19
use Acme\App\Core\Component\Blog\Domain\Post\PostId;
20
use Acme\App\Core\Component\User\Application\Repository\UserRepositoryInterface;
21
use Acme\App\Core\Component\User\Domain\User\User;
22
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId;
23
use Acme\App\Presentation\Api\GraphQl\Node\Post\PostViewModel;
24
use Acme\App\Presentation\Api\GraphQl\Node\User\AbstractUserViewModel;
25
use Acme\App\Presentation\Api\GraphQl\Node\User\UserResolver;
26
use Overblog\GraphQLBundle\Definition\Argument;
27
use Overblog\GraphQLBundle\Resolver\ResolverMap as BaseResolverMap;
28
use function array_map;
29
30
final class QueryResolverMap extends BaseResolverMap
31
{
32
    /**
33
     * @var UserRepositoryInterface
34
     */
35
    private $userRepository;
36
37
    /**
38
     * @var PostRepositoryInterface
39
     */
40
    private $postRepository;
41
42
    /**
43
     * @var UserResolver
44
     */
45
    private $userResolver;
46
47
    public function __construct(
48
        UserRepositoryInterface $userRepository,
49
        PostRepositoryInterface $postRepository,
50
        UserResolver $userResolver
51
    ) {
52
        $this->userRepository = $userRepository;
53
        $this->postRepository = $postRepository;
54
        $this->userResolver = $userResolver;
55
    }
56
57
    protected function map(): array
58
    {
59
        return [
60
            'Query' => [
61
                'me' => function ($value, Argument $args) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
                    return $this->userResolver->getUser();
63
                },
64
                'post' => function ($value, Argument $args) {
65
                    $post = $this->postRepository->find(new PostId($args['id']));
66
67
                    return PostViewModel::constructFromEntity($post);
68
                },
69
                'postList' => function () {
70
                    return array_map(
71
                        function (Post $post) {
72
                            return PostViewModel::constructFromEntity($post);
73
                        },
74
                        $this->postRepository->findAll()->toArray()
75
                    );
76
                },
77
                'user' => function ($value, Argument $args) {
78
                    $user = $this->userRepository->findOneById(new UserId($args['id']));
79
80
                    return AbstractUserViewModel::constructFromEntity($user);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Acme\App\Presentation\Ap...r\AbstractUserViewModel as the method constructFromEntity() does only exist in the following sub-classes of Acme\App\Presentation\Ap...r\AbstractUserViewModel: Acme\App\Presentation\Ap...er\Admin\AdminViewModel, Acme\App\Presentation\Ap...\Editor\EditorViewModel. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
81
                },
82
                'userList' => function () {
83
                    return array_map(
84
                        function (User $user) {
85
                            return AbstractUserViewModel::constructFromEntity($user);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Acme\App\Presentation\Ap...r\AbstractUserViewModel as the method constructFromEntity() does only exist in the following sub-classes of Acme\App\Presentation\Ap...r\AbstractUserViewModel: Acme\App\Presentation\Ap...er\Admin\AdminViewModel, Acme\App\Presentation\Ap...\Editor\EditorViewModel. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
86
                        },
87
                        $this->userRepository->findAll()->toArray()
88
                    );
89
                },
90
            ],
91
        ];
92
    }
93
}
94