Completed
Push — master ( d4bf05...95ad7b )
by Emil
02:54
created

QueueScheduler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 34.91 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 37
loc 106
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setDoctrine() 0 4 1
A setTaskManager() 0 4 1
A schedule() 0 11 2
A queue() 0 12 3
A checkNextRunDate() 18 18 2
A checkPreviousRunDate() 19 19 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\Queue;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\ORM\NoResultException;
7
use Glooby\TaskBundle\Entity\QueuedTask;
8
use Glooby\TaskBundle\Entity\QueuedTaskRepository;
9
use Glooby\TaskBundle\Manager\TaskManager;
10
use Glooby\TaskBundle\Model\ScheduleInterface;
11
12
/**
13
 * @author Emil Kilhage
14
 */
15
class QueueScheduler
16
{
17
    /**
18
     * @var ManagerRegistry
19
     */
20
    protected $doctrine;
21
22
    /**
23
     * @param ManagerRegistry $doctrine
24
     */
25
    public function setDoctrine($doctrine)
26
    {
27
        $this->doctrine = $doctrine;
28
    }
29
30
    /**
31
     * @var TaskManager
32
     */
33
    private $taskManager;
34
35
    /**
36
     * @param TaskManager $taskManager
37
     */
38
    public function setTaskManager(TaskManager $taskManager)
39
    {
40
        $this->taskManager = $taskManager;
41
    }
42
43
    /**
44
     *
45
     */
46
    public function schedule()
47
    {
48
        $repo = $this->doctrine->getManager()
49
            ->getRepository('GloobyTaskBundle:Schedule');
50
51
        foreach ($repo->findActive() as $schedule) {
52
            $this->queue($schedule);
53
        }
54
55
        $this->doctrine->getManager()->flush();
56
    }
57
58
    /**
59
     * @param ScheduleInterface $schedule
60
     */
61
    private function queue(ScheduleInterface $schedule)
62
    {
63
        $executeAt = $this->checkPreviousRunDate($schedule);
64
65
        if (null === $executeAt) {
66
            $executeAt = $this->checkNextRunDate($schedule);
67
        }
68
69
        if (null !== $executeAt) {
70
            $this->taskManager->queue($schedule->getName(), $executeAt, $schedule->getParams());
71
        }
72
    }
73
74
    /**
75
     * @param ScheduleInterface $schedule
76
     * @return \DateTime|null
77
     */
78 View Code Duplication
    private function checkNextRunDate(ScheduleInterface $schedule)
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...
79
    {
80
        /** @var QueuedTaskRepository $queueRepo */
81
        $queueRepo = $this->doctrine->getManager()
82
            ->getRepository('GloobyTaskBundle:QueuedTask');
83
84
        $executeAt = null;
85
        $expression = $schedule->parseExpression();
86
        $nextExecuteAt = $expression->getNextRunDate();
87
88
        try {
89
            $queueRepo->getByNameAndExecuteAt($schedule->getName(), $nextExecuteAt);
90
        } catch (NoResultException $e) {
91
            $executeAt = $nextExecuteAt;
92
        }
93
94
        return $executeAt;
95
    }
96
97
    /**
98
     * @param ScheduleInterface $schedule
99
     * @return \DateTime|null
100
     */
101 View Code Duplication
    private function checkPreviousRunDate(ScheduleInterface $schedule)
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...
102
    {
103
        /** @var QueuedTaskRepository $queueRepo */
104
        $queueRepo = $this->doctrine->getManager()
105
            ->getRepository('GloobyTaskBundle:QueuedTask');
106
107
        $expression = $schedule->parseExpression();
108
109
        $executeAt = null;
110
111
        try {
112
            $queueRepo->getByNameAndExecuteAtBeforeNow($schedule->getName());
113
            return $executeAt;
114
        } catch (NoResultException $e) {
115
            $executeAt = $expression->getPreviousRunDate();
116
        }
117
118
        return $executeAt;
119
    }
120
}
121