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