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\CommentListQueryInterface; |
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\QueryServiceRouterInterface; |
22
|
|
|
use Acme\App\Core\Port\Persistence\ResultCollectionInterface; |
23
|
|
|
|
24
|
|
|
final class CommentListQuery implements CommentListQueryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $includeAuthor = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var DqlQueryBuilderInterface |
33
|
|
|
*/ |
34
|
|
|
private $dqlQueryBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var QueryServiceRouterInterface |
38
|
|
|
*/ |
39
|
|
|
private $queryService; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
DqlQueryBuilderInterface $dqlQueryBuilder, |
43
|
|
|
QueryServiceRouterInterface $queryService |
44
|
|
|
) { |
45
|
|
|
$this->dqlQueryBuilder = $dqlQueryBuilder; |
46
|
|
|
$this->queryService = $queryService; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function includeAuthor(): CommentListQueryInterface |
50
|
|
|
{ |
51
|
|
|
$this->includeAuthor = true; |
52
|
|
|
|
53
|
|
|
return $this; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function execute(PostId $postId): ResultCollectionInterface |
57
|
|
|
{ |
58
|
|
|
$this->dqlQueryBuilder->create(Post::class, 'Post') |
59
|
|
|
->select( |
60
|
|
|
'Comment.id AS ' . self::KEY_ID, |
61
|
|
|
'Comment.publishedAt AS publishedAt', |
62
|
|
|
'Comment.content AS content' |
63
|
|
|
) |
64
|
|
|
->join('Post.comments', 'Comment') |
65
|
|
|
->where('Post.id = :postId') |
66
|
|
|
->setParameter('postId', $postId) |
67
|
|
|
->orderBy('publishedAt', 'DESC'); |
68
|
|
|
|
69
|
|
|
if ($this->includeAuthor) { |
70
|
|
|
$this->joinAuthor($this->dqlQueryBuilder); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->resetJoins(); |
74
|
|
|
|
75
|
|
|
return $this->queryService->query($this->dqlQueryBuilder->build()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function joinAuthor(DqlQueryBuilderInterface $queryBuilder): void |
79
|
|
|
{ |
80
|
|
|
$queryBuilder->addSelect('Author.fullName AS authorFullName') |
81
|
|
|
// This join with 'User:User' is the same as a join with User::class. The main difference is that this way |
82
|
|
|
// we are not depending directly on the User entity, but on a configurable alias. The advantage is that we |
83
|
|
|
// can change where the user data is stored and this query will remain the same. For example we could move |
84
|
|
|
// this component into a microservice, with its own curated user data, and we wouldn't need to change this |
85
|
|
|
// query, only the doctrine alias configuration. |
86
|
|
|
->join('User:User', 'Author', 'WITH', 'Author.id = Comment.authorId'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function resetJoins(): void |
90
|
|
|
{ |
91
|
|
|
$this->includeAuthor = false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.