Completed
Push — master ( 682c42...b25a4a )
by Emil
02:06
created

QueuedTaskRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 36 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 36
loc 100
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getByNameAndExecuteAt() 0 11 1
A getByNameAndExecuteAtBeforeNow() 0 12 1
A findQueued() 0 13 1
A isRunning() 18 18 2
A isQueued() 18 18 2

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 Glooby\TaskBundle\Entity;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\NoResultException;
7
use Glooby\TaskBundle\Model\QueuedTaskInterface;
8
9
/**
10
 * @author Emil Kilhage
11
 */
12
class QueuedTaskRepository extends EntityRepository
13
{
14
    /**
15
     * @param string $name
16
     * @param \DateTime $executeAt
17
     * @return QueuedTaskInterface
18
     */
19
    public function getByNameAndExecuteAt($name, \DateTime $executeAt)
20
    {
21
        return $this->getEntityManager()
22
            ->createQuery('SELECT r
23
              FROM GloobyTaskBundle:QueuedTask r
24
              WHERE r.name = :name AND r.executeAt = :executeAt')
25
            ->setParameter('name', $name)
26
            ->setParameter('executeAt', $executeAt)
27
            ->useQueryCache(true)
28
            ->getSingleResult();
29
    }
30
31
    /**
32
     * @param string $name
33
     * @return QueuedTaskInterface
34
     */
35
    public function getByNameAndExecuteAtBeforeNow($name)
36
    {
37
        return $this->getEntityManager()
38
            ->createQuery('SELECT r
39
              FROM GloobyTaskBundle:QueuedTask r
40
              WHERE r.name = :name AND r.executeAt <= :now')
41
            ->setParameter('name', $name)
42
            ->setParameter('now', new \DateTime())
43
            ->useQueryCache(true)
44
            ->setMaxResults(1)
45
            ->getSingleResult();
46
    }
47
48
    /**
49
     * @param int $limit
50
     * @return QueuedTaskInterface[]
51
     */
52
    public function findQueued($limit)
53
    {
54
        return $this->getEntityManager()
55
            ->createQuery('SELECT r
56
              FROM GloobyTaskBundle:QueuedTask r
57
              WHERE r.status = :status AND r.executeAt <= :now
58
              ORDER BY r.executeAt ASC')
59
            ->setParameter('status', QueuedTaskInterface::STATUS_QUEUED)
60
            ->setParameter('now', new \DateTime())
61
            ->setMaxResults($limit)
62
            ->useQueryCache(true)
63
            ->getResult();
64
    }
65
66
    /**
67
     * @param string $name
68
     * @return bool
69
     */
70 View Code Duplication
    public function isRunning($name)
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...
71
    {
72
        try {
73
            $this->getEntityManager()
74
                ->createQuery('SELECT r
75
                  FROM GloobyTaskBundle:QueuedTask r
76
                  WHERE r.name = :name AND r.status = :status')
77
                ->setParameter('name', $name)
78
                ->setParameter('status', QueuedTaskInterface::STATUS_RUNNING)
79
                ->useQueryCache(true)
80
                ->setMaxResults(1)
81
                ->getSingleResult();
82
83
            return true;
84
        } catch (NoResultException $e) {
85
            return false;
86
        }
87
    }
88
89
    /**
90
     * @param string $name
91
     * @return bool
92
     */
93 View Code Duplication
    public function isQueued($name)
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...
94
    {
95
        try {
96
            $this->getEntityManager()
97
                ->createQuery('SELECT r
98
                  FROM GloobyTaskBundle:QueuedTask r
99
                  WHERE r.name = :name AND r.status = :status')
100
                ->setParameter('name', $name)
101
                ->setParameter('status', QueuedTaskInterface::STATUS_QUEUED)
102
                ->useQueryCache(true)
103
                ->setMaxResults(1)
104
                ->getSingleResult();
105
106
            return true;
107
        } catch (NoResultException $e) {
108
            return false;
109
        }
110
    }
111
}
112