UserRepository   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 31.76 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

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

7 Methods

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

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\User\Application\Repository\UserRepositoryInterface;
18
use Acme\App\Core\Component\User\Domain\User\User;
19
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface;
20
use Acme\App\Core\Port\Persistence\PersistenceServiceInterface;
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
/**
26
 * This custom Doctrine repository is empty because so far we don't need any custom
27
 * method to query for application user information. But it's always a good practice
28
 * to define a custom repository that will be used when the application grows.
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 Herberto Graca <[email protected]>
35
 */
36
class UserRepository implements UserRepositoryInterface
37
{
38
    /**
39
     * @var DqlQueryBuilderInterface
40
     */
41
    private $dqlQueryBuilder;
42
43
    /**
44
     * @var QueryServiceRouterInterface
45
     */
46
    private $queryService;
47
48
    /**
49
     * @var PersistenceServiceInterface
50
     */
51
    private $persistenceService;
52
53
    public function __construct(
54
        DqlQueryBuilderInterface $dqlQueryBuilder,
55
        QueryServiceRouterInterface $queryService,
56
        PersistenceServiceInterface $persistenceService
57
    ) {
58
        $this->dqlQueryBuilder = $dqlQueryBuilder;
59
        $this->queryService = $queryService;
60
        $this->persistenceService = $persistenceService;
61
    }
62
63
    public function add(User $user): void
64
    {
65
        $this->persistenceService->upsert($user);
66
    }
67
68
    public function remove(User $user): void
69
    {
70
        $this->persistenceService->delete($user);
71
    }
72
73
    /**
74
     * @return User[]
75
     */
76
    public function findAll(array $orderByList = ['id' => 'DESC'], int $maxResults = null): ResultCollectionInterface
77
    {
78
        $this->dqlQueryBuilder->create(User::class);
79
80
        foreach ($orderByList as $property => $direction) {
81
            $this->dqlQueryBuilder->orderBy('User.' . $property, $direction);
82
        }
83
84
        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...
85
            $this->dqlQueryBuilder->setMaxResults($maxResults);
86
        }
87
88
        return $this->queryService->query($this->dqlQueryBuilder->build());
89
    }
90
91 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...
92
    {
93
        $dqlQuery = $this->dqlQueryBuilder->create(User::class)
94
            ->where('User.username = :username')
95
            ->setParameter('username', $username)
96
            ->build();
97
98
        return $this->queryService->query($dqlQuery)->getSingleResult();
99
    }
100
101 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...
102
    {
103
        $dqlQuery = $this->dqlQueryBuilder->create(User::class)
104
            ->where('User.email = :email')
105
            ->setParameter('email', $email)
106
            ->build();
107
108
        return $this->queryService->query($dqlQuery)->getSingleResult();
109
    }
110
111 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...
112
    {
113
        $dqlQuery = $this->dqlQueryBuilder->create(User::class)
114
            ->where('User.id = :id')
115
            ->setParameter('id', $id)
116
            ->build();
117
118
        return $this->queryService->query($dqlQuery)->getSingleResult();
119
    }
120
}
121