Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

Repository/EntityVersionLockRepository.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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