Completed
Push — master ( 55a9c0...ed128c )
by Emil
14:56
created

TaskManager::run()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 16
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
9
/**
10
 * @author Emil Kilhage
11
 */
12
class TaskManager
13
{
14
    /**
15
     * @var ManagerRegistry
16
     */
17
    protected $doctrine;
18
19
    /**
20
     * @param ManagerRegistry $doctrine
21
     */
22
    public function setDoctrine($doctrine)
23
    {
24
        $this->doctrine = $doctrine;
25
    }
26
27
    /**
28
     * @param string $service
29
     * @param \DateTime|null $executeAt
30
     * @param array|null $params
31
     * @return QueuedTask
32
     */
33
    public function queue(string $service, \DateTime $executeAt = null, array $params = null): QueuedTask
34
    {
35
        $run = new QueuedTask();
36
        $run->setName($service);
37
        $run->setParams($params);
38
        $run->setStatus(QueuedTask::STATUS_PENDING);
39
        $run->setExecuteAt(null === $executeAt ? new \DateTime() : $executeAt);
40
41
        try {
42
            $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...
43
                ->getRepository('GloobyTaskBundle:Schedule')
44
                ->findByName($service);
45
46
            $run->setSchedule($schedule);
47
        } catch (NoResultException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
48
49
        }
50
51
        $this->doctrine->getManager()->persist($run);
52
53
        return $run;
54
    }
55
56
    /**
57
     * @param QueuedTask $run
58
     */
59
    public function start(QueuedTask $run)
60
    {
61
        $run->setStatus(QueuedTask::STATUS_RUNNING);
62
        $run->setStarted(new \DateTime());
63
        $this->doctrine->getManager()->flush();
64
    }
65
66
    /**
67
     * @param string $service
68
     * @param array $params
69
     * @return QueuedTask
70
     */
71
    public function run(string $service, array $params = null): QueuedTask
72
    {
73
        $run = new QueuedTask();
74
        $run->setName($service);
75
        $run->setParams($params);
76
        $run->setStatus(QueuedTask::STATUS_RUNNING);
77
        $run->setExecuteAt(new \DateTime());
78
        $run->setStarted(new \DateTime());
79
80
        try {
81
            $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...
82
                ->getRepository('GloobyTaskBundle:Schedule')
83
                ->findByName($service);
84
85
            $run->setSchedule($schedule);
86
        } catch (NoResultException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
87
88
        }
89
90
        $this->doctrine->getManager()->persist($run);
91
        $this->doctrine->getManager()->flush();
92
93
        return $run;
94
    }
95
96
    /**
97
     * @param QueuedTask $run
98
     * @param $response
99
     */
100 View Code Duplication
    public function success(QueuedTask $run, $response)
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...
101
    {
102
        if (!empty($response)) {
103
            $run->setResult(print_r($response, true));
104
        }
105
106
        $run->setStatus(QueuedTask::STATUS_DONE);
107
        $run->setResolution(QueuedTask::RESOLUTION_SUCCESS);
108
        $run->setFinished(new \DateTime());
109
110
        $this->doctrine->getManager()->flush();
111
    }
112
113
    /**
114
     * @param QueuedTask $run
115
     * @param $response
116
     */
117 View Code Duplication
    public function failure(QueuedTask $run, $response)
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...
118
    {
119
        if (!empty($response)) {
120
            $run->setResult(print_r($response, true));
121
        }
122
123
        $run->setStatus(QueuedTask::STATUS_DONE);
124
        $run->setResolution(QueuedTask::RESOLUTION_FAILURE);
125
        $run->setFinished(new \DateTime());
126
127
        $this->doctrine->getManager()->flush();
128
    }
129
}
130