Test Failed
Push — dev ( ccede5...b27119 )
by Herberto
13:46
created

QueryResolverMap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 Overblog\GraphQLBundle\Definition\Argument;
26
use Overblog\GraphQLBundle\Resolver\ResolverMap as BaseResolverMap;
27
use function array_map;
28
29
final class QueryResolverMap extends BaseResolverMap
30
{
31
    /**
32
     * @var UserRepositoryInterface
33
     */
34
    private $userRepository;
35
36
    /**
37
     * @var PostRepositoryInterface
38
     */
39
    private $postRepository;
40
41
    public function __construct(
42
        UserRepositoryInterface $userRepository,
43
        PostRepositoryInterface $postRepository
44
    ) {
45
        $this->userRepository = $userRepository;
46
        $this->postRepository = $postRepository;
47
    }
48
49
    protected function map(): array
50
    {
51
        return [
52
            'Query' => [
53
                'post' => function ($value, Argument $args) {
54
                    $post = $this->postRepository->find(new PostId($args['id']));
55
56
                    return PostViewModel::constructFromEntity($post);
57
                },
58
                'postList' => function () {
59
                    return array_map(
60
                        function (Post $post) {
61
                            return PostViewModel::constructFromEntity($post);
62
                        },
63
                        $this->postRepository->findAll()->toArray()
64
                    );
65
                },
66
                'user' => function ($value, Argument $args) {
67
                    $user = $this->userRepository->findOneById(new UserId($args['id']));
68
69
                    return AbstractUserViewModel::constructFromEntity($user);
70
                },
71
                'userList' => function () {
72
                    return array_map(
73
                        function (User $user) {
74
                            return AbstractUserViewModel::constructFromEntity($user);
75
                        },
76
                        $this->userRepository->findAll()->toArray()
77
                    );
78
                },
79
            ],
80
        ];
81
    }
82
}
83