Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

EntityVersionLockRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 96.15 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 50
loc 52
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocksForLockableEntity() 19 19 2
A getExpiredLocks() 11 12 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
namespace Kunstmaan\AdminListBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Kunstmaan\AdminListBundle\Entity\LockableEntity;
7
use FOS\UserBundle\Model\User;
8
9
/**
10
 * EntityVersionLockRepository
11
 */
12 View Code Duplication
class EntityVersionLockRepository extends EntityRepository
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * Check if there is a entity lock that's not passed the threshold.
16
     *
17
     * @param LockableEntity $entity
18
     * @param int            $threshold
19
     * @param User           $userToExclude
0 ignored issues
show
Documentation introduced by
Should the type for parameter $userToExclude not be null|User?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
20
     *
21
     * @return EntityVersionLock[]
22
     */
23
    public function getLocksForLockableEntity(LockableEntity $entity, $threshold, User $userToExclude = null)
24
    {
25
        $qb = $this->createQueryBuilder('evl')
26
            ->select('evl')
27
            ->join('evl.lockableEntity', 'le')
28
            ->where('le.id = :e')
29
            ->andWhere('evl.createdAt > :date')
30
            ->setParameter('e', $entity->getId())
31
            ->setParameter('date', new \DateTime(sprintf('-%s seconds', $threshold)))
32
        ;
33
34
        if (!\is_null($userToExclude)) {
35
            $qb->andWhere('evl.owner <> :owner')
36
                ->setParameter('owner', $userToExclude->getUsername())
37
            ;
38
        }
39
40
        return $qb->getQuery()->getResult();
41
    }
42
43
    /**
44
     * Get locks that are passed the threshold.
45
     *
46
     * @param LockableEntity $entity
47
     * @param int            $threshold
48
     *
49
     * @return mixed
50
     */
51
    public function getExpiredLocks(LockableEntity $entity, $threshold)
52
    {
53
        $qb = $this->createQueryBuilder('evl')
54
            ->select('evl')
55
            ->join('evl.lockableEntity', 'le')
56
            ->where('le.id = :e')
57
            ->andWhere('evl.createdAt < :date')
58
            ->setParameter('e', $entity->getId())
59
            ->setParameter('date', new \DateTime(sprintf('-%s seconds', $threshold)));
60
61
        return $qb->getQuery()->getResult();
62
    }
63
}
64