Completed
Push — master ( 03835b...a56aa2 )
by Emil
02:19
created

TaskManager::populateSchedule()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace Glooby\TaskBundle\Manager;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\ORM\NoResultException;
7
use Glooby\TaskBundle\Entity\QueuedTask;
8
use Glooby\TaskBundle\Model\QueuedTaskInterface;
9
10
/**
11
 * @author Emil Kilhage
12
 */
13
class TaskManager
14
{
15
    /**
16
     * @var ManagerRegistry
17
     */
18
    protected $doctrine;
19
20
    /**
21
     * @param ManagerRegistry $doctrine
22
     */
23
    public function setDoctrine($doctrine)
24
    {
25
        $this->doctrine = $doctrine;
26
    }
27
28
    /**
29
     * @param string $service
30
     * @param \DateTime|null $executeAt
31
     * @param array|null $params
32
     * @return QueuedTaskInterface
33
     */
34
    public function queue(string $service, \DateTime $executeAt = null, array $params = null)
35
    {
36
        $run = new QueuedTask($service, $params, $executeAt);
37
        $this->populateSchedule($run, $service);
38
        $this->doctrine->getManager()->persist($run);
39
        return $run;
40
    }
41
42
    /**
43
     * @param QueuedTaskInterface $run
44
     */
45
    public function start(QueuedTaskInterface $run)
46
    {
47
        $run->start();
48
        $this->doctrine->getManager()->flush();
49
    }
50
51
    /**
52
     * @param string $service
53
     * @param array $params
54
     * @return QueuedTaskInterface
55
     */
56
    public function run(string $service, array $params = null)
57
    {
58
        $run = new QueuedTask($service, $params);
59
        $run->start();
60
        $this->populateSchedule($run, $service);
61
62
        $this->doctrine->getManager()->persist($run);
63
        $this->doctrine->getManager()->flush();
64
65
        return $run;
66
    }
67
68
    /**
69
     * @param QueuedTaskInterface $run
70
     * @param $response
71
     */
72
    public function success(QueuedTaskInterface $run, $response)
73
    {
74
        $run->success($response);
75
        $this->doctrine->getManager()->flush();
76
    }
77
78
    /**
79
     * @param QueuedTaskInterface $run
80
     * @param $response
81
     */
82
    public function failure(QueuedTaskInterface $run, $response)
83
    {
84
        $run->failure($response);
85
        $this->doctrine->getManager()->flush();
86
    }
87
88
    /**
89
     * @param QueuedTaskInterface $run
90
     * @param string $service
91
     */
92
    private function populateSchedule(QueuedTaskInterface $run, $service)
93
    {
94
        try {
95
            $schedule = $this->doctrine->getManager()
0 ignored issues
show
Bug introduced by
The method findByName() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
96
                ->getRepository('GloobyTaskBundle:Schedule')
97
                ->findByName($service);
98
99
            $run->setSchedule($schedule);
100
        } catch (NoResultException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
101
102
        }
103
    }
104
}
105