UserRepository::findByResetPasswordToken()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 22
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 22
loc 22
rs 8.9197
cc 4
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace Sinergi\Users\Doctrine\User;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Interop\Container\ContainerInterface;
9
use Sinergi\Users\Container;
10
use Sinergi\Users\Group\GroupRepositoryInterface;
11
use Sinergi\Users\Session\SessionRepositoryInterface;
12
use Sinergi\Users\User\UserEntityInterface;
13
use Sinergi\Users\User\UserRepositoryInterface;
14
15
class UserRepository extends EntityRepository implements UserRepositoryInterface
16
{
17
    private $container;
18
19
    public function __construct(EntityManager $em, ClassMetadata $class, ContainerInterface $container)
20
    {
21
        parent::__construct($em, $class);
22
        if ($container instanceof Container) {
23
            $this->container = $container;
24
        } else {
25
            $this->container = new Container($container);
26
        }
27
    }
28
29
    /** @return UserEntity */
30 View Code Duplication
    public function findByEmail(string $email)
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...
31
    {
32
        $result = $this->createQueryBuilder('u')
33
            ->where('u.email = :email')
34
            ->setParameter('email', $email)
35
            ->getQuery()
36
            ->getResult();
37
38
39
        if ($result && $result[0]) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
            /** @var UserEntityInterface $user */
41
            $user = $result[0];
42
            $user->setGroupRepository($this->container->get(GroupRepositoryInterface::class));
43
            $user->setSessionRepository($this->container->get(SessionRepositoryInterface::class));
44
            return $user;
45
        }
46
47
        return null;
48
    }
49
50
    /** @return UserEntity */
51 View Code Duplication
    public function findById(int $id)
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...
52
    {
53
        $result = $this->createQueryBuilder('u')
54
            ->where('u.id = :id')
55
            ->setParameter('id', $id)
56
            ->getQuery()
57
            ->getResult();
58
59
        if ($result && $result[0]) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
60
            /** @var UserEntityInterface $user */
61
            $user = $result[0];
62
            $user->setGroupRepository($this->container->get(GroupRepositoryInterface::class));
63
            $user->setSessionRepository($this->container->get(SessionRepositoryInterface::class));
64
            return $user;
65
        }
66
67
        return null;
68
    }
69
70
    /** @return UserEntityInterface[] */
71 View Code Duplication
    public function findByGroupId(int $id)
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...
72
    {
73
        $result = $this->createQueryBuilder('u')
74
            ->where('u.groupId = :groupId')
75
            ->setParameter('groupId', $id)
76
            ->getQuery()
77
            ->getResult();
78
79
        if ($result && $result[0]) {
80
            /** @var UserEntityInterface $user */
81
            foreach ($result as $user) {
82
                $user->setGroupRepository($this->container->get(GroupRepositoryInterface::class));
83
                $user->setSessionRepository($this->container->get(SessionRepositoryInterface::class));
84
            }
85
            return $result;
86
        }
87
88
        return [];
89
    }
90
91
    /** @return UserEntityInterface */
92 View Code Duplication
    public function findByResetPasswordToken(string $token)
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...
93
    {
94
        if (empty($token)) {
95
            return null;
96
        }
97
98
        $result = $this->createQueryBuilder('u')
99
            ->where('u.passwordResetToken = :token')
100
            ->setParameter('token', $token)
101
            ->getQuery()
102
            ->getResult();
103
104
        if ($result && $result[0]) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
            /** @var UserEntityInterface $user */
106
            $user = $result[0];
107
            $user->setGroupRepository($this->container->get(GroupRepositoryInterface::class));
108
            $user->setSessionRepository($this->container->get(SessionRepositoryInterface::class));
109
            return $user;
110
        }
111
112
        return null;
113
    }
114
115
    public function save(UserEntityInterface $user)
116
    {
117
        $this->getEntityManager()->persist($user);
118
        $this->getEntityManager()->flush($user);
119
    }
120
}
121