|
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\User\Application\Repository\DQL; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Component\Blog\Domain\Post\Comment\Comment; |
|
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\Component\User\Application\Repository\UserRepositoryInterface; |
|
21
|
|
|
use Acme\App\Core\Component\User\Domain\User\User; |
|
22
|
|
|
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface; |
|
23
|
|
|
use Acme\App\Core\Port\Persistence\PersistenceServiceInterface; |
|
24
|
|
|
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface; |
|
25
|
|
|
use Acme\App\Core\Port\Persistence\ResultCollectionInterface; |
|
26
|
|
|
use Acme\App\Core\SharedKernel\Component\Blog\Domain\Post\Comment\CommentId; |
|
27
|
|
|
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId; |
|
28
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* This custom Doctrine repository is empty because so far we don't need any custom |
|
32
|
|
|
* method to query for application user information. But it's always a good practice |
|
33
|
|
|
* to define a custom repository that will be used when the application grows. |
|
34
|
|
|
* |
|
35
|
|
|
* See https://symfony.com/doc/current/doctrine/repository.html |
|
36
|
|
|
* |
|
37
|
|
|
* @author Ryan Weaver <[email protected]> |
|
38
|
|
|
* @author Javier Eguiluz <[email protected]> |
|
39
|
|
|
* @author Herberto Graca <[email protected]> |
|
40
|
|
|
*/ |
|
41
|
|
|
class UserRepository implements UserRepositoryInterface |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* @var DqlQueryBuilderInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $dqlQueryBuilder; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var QueryServiceRouterInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $queryService; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var PersistenceServiceInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $persistenceService; |
|
57
|
|
|
|
|
58
|
|
|
public function __construct( |
|
59
|
|
|
DqlQueryBuilderInterface $dqlQueryBuilder, |
|
60
|
|
|
QueryServiceRouterInterface $queryService, |
|
61
|
|
|
PersistenceServiceInterface $persistenceService |
|
62
|
|
|
) { |
|
63
|
|
|
$this->dqlQueryBuilder = $dqlQueryBuilder; |
|
64
|
|
|
$this->queryService = $queryService; |
|
65
|
|
|
$this->persistenceService = $persistenceService; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function add(User $user): void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->persistenceService->upsert($user); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function remove(User $user): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->persistenceService->delete($user); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return User[] |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
public function findAll(array $orderByList = ['id' => 'DESC'], int $maxResults = null): ResultCollectionInterface |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$this->dqlQueryBuilder->create(User::class); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($orderByList as $property => $direction) { |
|
86
|
|
|
$this->dqlQueryBuilder->orderBy('User.' . $property, $direction); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($maxResults) { |
|
|
|
|
|
|
90
|
|
|
$this->dqlQueryBuilder->setMaxResults($maxResults); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->queryService->query($this->dqlQueryBuilder->build()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
public function findOneByUsername(string $username): User |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$dqlQuery = $this->dqlQueryBuilder->create(User::class) |
|
99
|
|
|
->where('User.username = :username') |
|
100
|
|
|
->setParameter('username', $username) |
|
101
|
|
|
->build(); |
|
102
|
|
|
|
|
103
|
|
|
return $this->queryService->query($dqlQuery)->getSingleResult(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
View Code Duplication |
public function findOneByEmail(string $email): User |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$dqlQuery = $this->dqlQueryBuilder->create(User::class) |
|
109
|
|
|
->where('User.email = :email') |
|
110
|
|
|
->setParameter('email', $email) |
|
111
|
|
|
->build(); |
|
112
|
|
|
|
|
113
|
|
|
return $this->queryService->query($dqlQuery)->getSingleResult(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
public function findOneById(UserId $id): User |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
$dqlQuery = $this->dqlQueryBuilder->create(User::class) |
|
119
|
|
|
->where('User.id = :id') |
|
120
|
|
|
->setParameter('id', $id) |
|
121
|
|
|
->build(); |
|
122
|
|
|
|
|
123
|
|
|
return $this->queryService->query($dqlQuery)->getSingleResult(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return User[] |
|
128
|
|
|
*/ |
|
129
|
|
View Code Duplication |
public function findAllByCommentId( |
|
|
|
|
|
|
130
|
|
|
CommentId $commentId, |
|
131
|
|
|
array $orderByList = ['id' => 'DESC'], |
|
132
|
|
|
int $maxResults = null |
|
133
|
|
|
): ResultCollectionInterface { |
|
134
|
|
|
|
|
135
|
|
|
$this->dqlQueryBuilder->create(User::class); |
|
136
|
|
|
|
|
137
|
|
|
$this->dqlQueryBuilder->innerJoin(Comment::class, 'Comment', Join::WITH, 'Comment.authorId = User.id') |
|
138
|
|
|
->where('Comment.id = :comment') |
|
139
|
|
|
->setParameter('comment', $commentId); |
|
140
|
|
|
|
|
141
|
|
|
foreach ($orderByList as $property => $direction) { |
|
142
|
|
|
$this->dqlQueryBuilder->orderBy('User.' . $property, $direction); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if ($maxResults) { |
|
|
|
|
|
|
146
|
|
|
$this->dqlQueryBuilder->setMaxResults($maxResults); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $this->queryService->query($this->dqlQueryBuilder->build()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return User[] |
|
154
|
|
|
*/ |
|
155
|
|
View Code Duplication |
public function findAllByPostId( |
|
|
|
|
|
|
156
|
|
|
PostId $postId, |
|
157
|
|
|
array $orderByList = ['id' => 'DESC'], |
|
158
|
|
|
int $maxResults = null |
|
159
|
|
|
): ResultCollectionInterface { |
|
160
|
|
|
|
|
161
|
|
|
$this->dqlQueryBuilder->create(User::class); |
|
162
|
|
|
|
|
163
|
|
|
$this->dqlQueryBuilder->innerJoin(Post::class, 'Post', Join::WITH, 'Post.authorId = User.id') |
|
164
|
|
|
->where('Post.id = :post') |
|
165
|
|
|
->setParameter('post', $postId); |
|
166
|
|
|
|
|
167
|
|
|
foreach ($orderByList as $property => $direction) { |
|
168
|
|
|
$this->dqlQueryBuilder->orderBy('User.' . $property, $direction); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if ($maxResults) { |
|
|
|
|
|
|
172
|
|
|
$this->dqlQueryBuilder->setMaxResults($maxResults); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $this->queryService->query($this->dqlQueryBuilder->build()); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
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.