Test Failed
Push — dev ( ccede5...b27119 )
by Herberto
13:46
created

UserRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 137
Duplicated Lines 62.04 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 4
dl 85
loc 137
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A add() 0 4 1
A remove() 0 4 1
A findAll() 14 14 3
A findOneByUsername() 9 9 1
A findOneByEmail() 9 9 1
A findOneById() 9 9 1
A findAllByCommentId() 22 22 3
A findAllByPostId() 22 22 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
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...
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) {
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...
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
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...
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
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...
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
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...
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(
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...
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) {
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...
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(
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...
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) {
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...
172
            $this->dqlQueryBuilder->setMaxResults($maxResults);
173
        }
174
175
        return $this->queryService->query($this->dqlQueryBuilder->build());
176
    }
177
}
178