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 |
|
|
|
|
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
|
|
|
|
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.