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

QueueScheduler::queue()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
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