Completed
Push — master ( a5d24e...028b40 )
by Gabriel
02:43
created

SessionRepository::findByUserId()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 18
loc 18
rs 9.2
cc 4
eloc 11
nc 3
nop 1
1
<?php
2
3
namespace Sinergi\Users\Doctrine\Session;
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\Session\SessionEntityInterface;
11
use Sinergi\Users\Session\SessionRepositoryInterface;
12
use Sinergi\Users\User\UserRepositoryInterface;
13
14
class SessionRepository extends EntityRepository implements SessionRepositoryInterface
15
{
16
    private $container;
17
18 View Code Duplication
    public function __construct(EntityManager $em, ClassMetadata $class, ContainerInterface $container)
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...
19
    {
20
        parent::__construct($em, $class);
21
        if ($container instanceof Container) {
22
            $this->container = $container;
23
        } else {
24
            $this->container = new Container($container);
25
        }
26
    }
27
28
    public function save(SessionEntityInterface $session)
29
    {
30
        $this->getEntityManager()->persist($session);
31
        $this->getEntityManager()->flush($session);
32
    }
33
34
    /** @return SessionEntityInterface */
35 View Code Duplication
    public function findById(string $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...
36
    {
37
        $result = $this->createQueryBuilder('s')
38
            ->where('s.id = :id')
39
            ->setParameter('id', $id)
40
            ->getQuery()
41
            ->getResult();
42
43
        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...
44
            /** @var SessionEntityInterface $session */
45
            $session = $result[0];
46
            $session->setUserRepository($this->container->get(UserRepositoryInterface::class));
47
            return $session;
48
        }
49
50
        return null;
51
    }
52
53
    /** @return SessionEntityInterface[] */
54 View Code Duplication
    public function findByUserId(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...
55
    {
56
        $result = $this->createQueryBuilder('s')
57
            ->where('s.userId = :userId')
58
            ->setParameter('userId', $id)
59
            ->getQuery()
60
            ->getResult();
61
62
        if ($result && $result[0]) {
63
            /** @var SessionEntityInterface $session */
64
            foreach ($result as $session) {
65
                $session->setUserRepository($this->container->get(UserRepositoryInterface::class));
66
            }
67
            return $result;
68
        }
69
70
        return [];
71
    }
72
73
    public function delete(SessionEntityInterface $session)
74
    {
75
        $this->getEntityManager()->remove($session);
76
        $this->getEntityManager()->flush($session);
77
    }
78
}
79