CommentListQuery::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Acme\App\Core\Component\...ry\DQL\CommentListQuery) is incompatible with the return type declared by the interface Acme\App\Core\Component\...nterface::includeAuthor of type self.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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