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

PostAuthorsResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
dl 5
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Node\Post\Connection\Author;
16
17
use Acme\App\Core\Component\Blog\Domain\Post\PostId;
18
use Acme\App\Core\Component\User\Application\Repository\UserRepositoryInterface;
19
use Acme\App\Core\Component\User\Domain\User\User;
20
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException;
21
use Acme\App\Presentation\Api\GraphQl\Node\User\AbstractUserViewModel;
22
use Doctrine\Common\Collections\Collection;
23
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
24
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder;
25
use function array_map;
26
use function count;
27
28 View Code Duplication
final class PostAuthorsResolver
0 ignored issues
show
Duplication introduced by
This class 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...
29
{
30
    /**
31
     * @var UserRepositoryInterface
32
     */
33
    private $userRepository;
34
35
    public function __construct(
36
        UserRepositoryInterface $userRepository
37
    ) {
38
        $this->userRepository = $userRepository;
39
    }
40
41
    public function getPostAuthorsConnection(PostId $postId): Connection
42
    {
43
        try {
44
            /** @var Collection $authorList */
45
            $authorList = $this->userRepository->findAllByPostId($postId);
46
        } catch (EmptyQueryResultException $e) {
47
            return ConnectionBuilder::connectionFromArray([]);
48
        }
49
50
        $authorViewModelList = array_map(
51
            function (User $author) {
52
                return AbstractUserViewModel::constructFromEntity($author);
53
            },
54
            $authorList->toArray()
55
        );
56
57
        return ConnectionBuilder::connectionFromArray($authorViewModelList);
58
    }
59
60
    public function countEdges(Connection $connection): int
61
    {
62
        return count($connection->edges);
63
    }
64
}
65