|
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\PostRepositoryInterface; |
|
18
|
|
|
use Acme\App\Core\Component\Blog\Domain\Post\Post; |
|
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\PersistenceServiceInterface; |
|
22
|
|
|
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface; |
|
23
|
|
|
use Acme\App\Core\Port\Persistence\ResultCollectionInterface; |
|
24
|
|
|
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* This custom Doctrine repository contains some methods which are useful when |
|
28
|
|
|
* querying for blog post information. |
|
29
|
|
|
* |
|
30
|
|
|
* See https://symfony.com/doc/current/doctrine/repository.html |
|
31
|
|
|
* |
|
32
|
|
|
* @author Ryan Weaver <[email protected]> |
|
33
|
|
|
* @author Javier Eguiluz <[email protected]> |
|
34
|
|
|
* @author Yonel Ceruto <[email protected]> |
|
35
|
|
|
* @author Herberto Graca <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class PostRepository implements PostRepositoryInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var DqlQueryBuilderInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $dqlQueryBuilder; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var QueryServiceRouterInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $queryService; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var PersistenceServiceInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $persistenceService; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct( |
|
55
|
|
|
DqlQueryBuilderInterface $dqlQueryBuilder, |
|
56
|
|
|
QueryServiceRouterInterface $queryService, |
|
57
|
|
|
PersistenceServiceInterface $persistenceService |
|
58
|
|
|
) { |
|
59
|
|
|
$this->dqlQueryBuilder = $dqlQueryBuilder; |
|
60
|
|
|
$this->queryService = $queryService; |
|
61
|
|
|
$this->persistenceService = $persistenceService; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return Post[] |
|
66
|
|
|
*/ |
|
67
|
|
View Code Duplication |
public function findAll(array $orderByList = ['id' => 'DESC'], int $maxResults = null): ResultCollectionInterface |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$this->dqlQueryBuilder->create(Post::class); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($orderByList as $property => $direction) { |
|
72
|
|
|
$this->dqlQueryBuilder->orderBy('Post.' . $property, $direction); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($maxResults) { |
|
|
|
|
|
|
76
|
|
|
$this->dqlQueryBuilder->setMaxResults($maxResults); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->queryService->query($this->dqlQueryBuilder->build()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return Post[] |
|
84
|
|
|
*/ |
|
85
|
|
View Code Duplication |
public function findAllByUserId( |
|
|
|
|
|
|
86
|
|
|
UserId $userId, |
|
87
|
|
|
array $orderByList = ['publishedAt' => 'DESC'], |
|
88
|
|
|
int $maxResults = null |
|
89
|
|
|
): ResultCollectionInterface { |
|
90
|
|
|
$this->dqlQueryBuilder->create(Post::class); |
|
91
|
|
|
|
|
92
|
|
|
$this->dqlQueryBuilder->where('Post.authorId = :user') |
|
93
|
|
|
->setParameter('user', $userId); |
|
94
|
|
|
|
|
95
|
|
|
foreach ($orderByList as $property => $direction) { |
|
96
|
|
|
$this->dqlQueryBuilder->orderBy('Post.' . $property, $direction); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ($maxResults) { |
|
|
|
|
|
|
100
|
|
|
$this->dqlQueryBuilder->setMaxResults($maxResults); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->queryService->query($this->dqlQueryBuilder->build()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function find(PostId $id): Post |
|
107
|
|
|
{ |
|
108
|
|
|
$dqlQuery = $this->dqlQueryBuilder->create(Post::class) |
|
109
|
|
|
->where('Post.id = :id') |
|
110
|
|
|
->setParameter('id', $id) |
|
111
|
|
|
->build(); |
|
112
|
|
|
|
|
113
|
|
|
return $this->queryService->query($dqlQuery)->getSingleResult(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function findBySlug(string $slug): Post |
|
117
|
|
|
{ |
|
118
|
|
|
$dqlQuery = $this->dqlQueryBuilder->create(Post::class) |
|
119
|
|
|
->where('Post.slug = :slug') |
|
120
|
|
|
->setParameter('slug', $slug) |
|
121
|
|
|
->build(); |
|
122
|
|
|
|
|
123
|
|
|
return $this->queryService->query($dqlQuery)->getSingleResult(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function add(Post $entity): void |
|
127
|
|
|
{ |
|
128
|
|
|
$this->persistenceService->upsert($entity); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function remove(Post $entity): void |
|
132
|
|
|
{ |
|
133
|
|
|
// Delete the tags associated with this blog post. This is done automatically |
|
134
|
|
|
// by Doctrine, except for SQLite (the database used in this application) |
|
135
|
|
|
// because foreign key support is not enabled by default in SQLite |
|
136
|
|
|
$entity->clearTags(); |
|
137
|
|
|
|
|
138
|
|
|
$this->persistenceService->delete($entity); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
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.