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

Repository/EntityVersionLockRepository.php (1 issue)

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