|
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\Comment\Connection\Author; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Component\Blog\Application\Query\FindAuthorIdByCommentIdQueryInterface; |
|
18
|
|
|
use Acme\App\Core\Component\User\Application\Repository\UserRepositoryInterface; |
|
19
|
|
|
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException; |
|
20
|
|
|
use Acme\App\Core\SharedKernel\Component\Blog\Domain\Post\Comment\CommentId; |
|
21
|
|
|
use Acme\App\Presentation\Api\GraphQl\Node\User\AbstractUserViewModel; |
|
22
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection; |
|
23
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder; |
|
24
|
|
|
use function count; |
|
25
|
|
|
|
|
26
|
|
View Code Duplication |
final class CommentAuthorsResolver |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var UserRepositoryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $userRepository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var FindAuthorIdByCommentIdQueryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $findAuthorIdByCommentIdQuery; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct( |
|
39
|
|
|
UserRepositoryInterface $userRepository, |
|
40
|
|
|
FindAuthorIdByCommentIdQueryInterface $findAuthorIdByCommentIdQuery |
|
41
|
|
|
) { |
|
42
|
|
|
$this->userRepository = $userRepository; |
|
43
|
|
|
$this->findAuthorIdByCommentIdQuery = $findAuthorIdByCommentIdQuery; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getCommentAuthorsConnection(CommentId $commentId): Connection |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
|
|
$author = $this->userRepository->findOneById( |
|
50
|
|
|
$this->findAuthorIdByCommentIdQuery->execute($commentId) |
|
51
|
|
|
); |
|
52
|
|
|
} catch (EmptyQueryResultException $e) { |
|
53
|
|
|
return ConnectionBuilder::connectionFromArray([]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return ConnectionBuilder::connectionFromArray( |
|
57
|
|
|
[ |
|
58
|
|
|
AbstractUserViewModel::constructFromEntity($author), |
|
|
|
|
|
|
59
|
|
|
] |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function countEdges(Connection $connection): int |
|
64
|
|
|
{ |
|
65
|
|
|
return count($connection->edges); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
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.