Test Setup Failed
Push — graphql_api ( 4ff753 )
by Herberto
07:48 queued 01:35
created

PostRepository::findAllByUserId()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 4
nop 3
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxResults of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxResults of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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