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

PostCommentsResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A getPostCommentsConnection() 18 18 2
A countEdges() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Comment;
16
17
use Acme\App\Core\Component\Blog\Application\Repository\CommentRepositoryInterface;
18
use Acme\App\Core\Component\Blog\Domain\Post\Comment\Comment;
19
use Acme\App\Core\Component\Blog\Domain\Post\PostId;
20
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException;
21
use Acme\App\Presentation\Api\GraphQl\Node\Comment\CommentViewModel;
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 PostCommentsResolver
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 CommentRepositoryInterface
32
     */
33
    private $commentRepository;
34
35
    public function __construct(
36
        CommentRepositoryInterface $commentRepository
37
    ) {
38
        $this->commentRepository = $commentRepository;
39
    }
40
41
    public function getPostCommentsConnection(PostId $postId): Connection
42
    {
43
        try {
44
            /** @var Collection $postCommentList */
45
            $postCommentList = $this->commentRepository->findAllByPostId($postId);
46
        } catch (EmptyQueryResultException $e) {
47
            return ConnectionBuilder::connectionFromArray([]);
48
        }
49
50
        $commentViewModelList = array_map(
51
            function (Comment $comment) {
52
                return CommentViewModel::constructFromEntity($comment);
53
            },
54
            $postCommentList->toArray()
55
        );
56
57
        return ConnectionBuilder::connectionFromArray($commentViewModelList);
58
    }
59
60
    public function countEdges(Connection $connection): int
61
    {
62
        return count($connection->edges);
63
    }
64
}
65