|
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\Query\DQL; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Component\Blog\Application\Query\PostListQueryInterface; |
|
18
|
|
|
use Acme\App\Core\Component\Blog\Domain\Post\Post; |
|
19
|
|
|
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface; |
|
20
|
|
|
use Acme\App\Core\Port\Persistence\QueryServiceInterface; |
|
21
|
|
|
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface; |
|
22
|
|
|
use Acme\App\Core\Port\Persistence\ResultCollectionInterface; |
|
23
|
|
|
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId; |
|
24
|
|
|
|
|
25
|
|
|
final class PostListQuery implements PostListQueryInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var DqlQueryBuilderInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $dqlQueryBuilder; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var QueryServiceInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $queryService; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
DqlQueryBuilderInterface $dqlQueryBuilder, |
|
39
|
|
|
QueryServiceRouterInterface $queryService |
|
40
|
|
|
) { |
|
41
|
|
|
$this->dqlQueryBuilder = $dqlQueryBuilder; |
|
42
|
|
|
$this->queryService = $queryService; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Since this class only has one public method, it makes sense that it is designed as a callable, using the |
|
47
|
|
|
* magic method name `__invoke()`, instead of having a single public method called `execute()` which adds nothing |
|
48
|
|
|
* to code readability. |
|
49
|
|
|
* However, by using `__invoke()` we lose code completion, so in the end I prefer to use this `execute()` method. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function execute(UserId $userId): ResultCollectionInterface |
|
52
|
|
|
{ |
|
53
|
|
|
$dqlQuery = $this->dqlQueryBuilder->create(Post::class, 'Post') |
|
54
|
|
|
->select( |
|
55
|
|
|
'Post.id', |
|
56
|
|
|
'Post.title', |
|
57
|
|
|
'Post.publishedAt' |
|
58
|
|
|
) |
|
59
|
|
|
->where('Post.authorId = :authorId') |
|
60
|
|
|
->orderBy('Post.publishedAt', 'DESC') |
|
61
|
|
|
->setParameter('authorId', $userId) |
|
62
|
|
|
->build(); |
|
63
|
|
|
|
|
64
|
|
|
return $this->queryService->query($dqlQuery); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..