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\Core\Component\Blog\Application\Repository\DQL; |
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\DQL\DqlQueryBuilderInterface; |
21
|
|
|
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface; |
22
|
|
|
use Acme\App\Core\Port\Persistence\ResultCollectionInterface; |
23
|
|
|
|
24
|
|
|
class CommentRepository implements CommentRepositoryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var DqlQueryBuilderInterface |
28
|
|
|
*/ |
29
|
|
|
private $dqlQueryBuilder; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var QueryServiceRouterInterface |
33
|
|
|
*/ |
34
|
|
|
private $queryService; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
DqlQueryBuilderInterface $dqlQueryBuilder, |
38
|
|
|
QueryServiceRouterInterface $queryService |
39
|
|
|
) { |
40
|
|
|
$this->dqlQueryBuilder = $dqlQueryBuilder; |
41
|
|
|
$this->queryService = $queryService; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Comment[] |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function findAllByPostId( |
|
|
|
|
48
|
|
|
PostId $postId, |
49
|
|
|
array $orderByList = ['publishedAt' => 'DESC'], |
50
|
|
|
int $maxResults = null |
51
|
|
|
): ResultCollectionInterface { |
52
|
|
|
$this->dqlQueryBuilder->create(Comment::class); |
53
|
|
|
|
54
|
|
|
$this->dqlQueryBuilder->where('Comment.post = :post') |
55
|
|
|
->setParameter('post', $postId); |
56
|
|
|
|
57
|
|
|
foreach ($orderByList as $property => $direction) { |
58
|
|
|
$this->dqlQueryBuilder->orderBy('Comment.' . $property, $direction); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($maxResults) { |
|
|
|
|
62
|
|
|
$this->dqlQueryBuilder->setMaxResults($maxResults); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->queryService->query($this->dqlQueryBuilder->build()); |
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.