Completed
Push — 0.4.5 ( ab71c2...f33756 )
by Peter
17:25
created

Task::getLastRun()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * AnimeDb package
4
 *
5
 * @package   AnimeDb
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\AppBundle\Entity;
12
13
use Doctrine\ORM\Mapping as ORM;
14
use Symfony\Component\Validator\Constraints as Assert;
15
use Symfony\Component\Validator\Context\ExecutionContextInterface;
16
17
/**
18
 * Task for Task Scheduler
19
 *
20
 * @ORM\Entity
21
 * @ORM\Table(name="task", indexes={
22
 *   @ORM\Index(name="idx_task_next_start", columns={"next_run", "status"})
23
 * })
24
 * @Assert\Callback(methods={"isModifyValid"})
25
 * @ORM\Entity(repositoryClass="AnimeDb\Bundle\AppBundle\Repository\Task")
26
 *
27
 * @package AnimeDb\Bundle\AppBundle\Entity
28
 * @author  Peter Gribanov <[email protected]>
29
 */
30
class Task
31
{
32
    /**
33
     * @var int
34
     */
35
    const STATUS_ENABLED = 1;
36
37
    /**
38
     * @var int
39
     */
40
    const STATUS_DISABLED = 0;
41
42
    /**
43
     * @ORM\Id
44
     * @ORM\GeneratedValue
45
     * @ORM\Column(type="integer")
46
     *
47
     * @var int
48
     */
49
    protected $id;
50
51
    /**
52
     * @ORM\Column(type="string", length=128)
53
     *
54
     * @var string
55
     */
56
    protected $command = '';
57
58
    /**
59
     * @ORM\Column(type="datetime", nullable=true)
60
     * @Assert\DateTime()
61
     *
62
     * @var \DateTime|null
63
     */
64
    protected $last_run;
65
66
    /**
67
     * @ORM\Column(type="datetime")
68
     * @Assert\DateTime()
69
     *
70
     * @var \DateTime
71
     */
72
    protected $next_run;
73
74
    /**
75
     * A date/time string
76
     *
77
     * Valid formats are explained in Date and Time Formats.
78
     *
79
     * @link http://www.php.net/manual/en/datetime.formats.php
80
     * @ORM\Column(type="string", length=128, nullable=true)
81
     *
82
     * @var string
83
     */
84
    protected $modify = '';
85
86
    /**
87
     * @ORM\Column(type="integer")
88
     * @Assert\Choice(callback = "getStatuses")
89
     *
90
     * @var int
91
     */
92
    protected $status = self::STATUS_DISABLED;
93
94 27
    public function __construct()
95
    {
96 27
        $this->next_run = new \DateTime();
97 27
    }
98
99
    /**
100
     * @param string $command
101
     *
102
     * @return Task
103
     */
104 1
    public function setCommand($command)
105
    {
106 1
        $this->command = $command;
107 1
        return $this;
108
    }
109
110
    /**
111
     * @return string
112
     */
113 1
    public function getCommand()
114
    {
115 1
        return $this->command;
116
    }
117
118
    /**
119
     * @return int
120
     */
121 1
    public function getId()
122
    {
123 1
        return $this->id;
124
    }
125
126
    /**
127
     * @param \DateTime $last_run
128
     *
129
     * @return Task
130
     */
131 4
    public function setLastRun(\DateTime $last_run)
132
    {
133 4
        $this->last_run = clone $last_run;
134 4
        return $this;
135
    }
136
137
    /**
138
     * @return \DateTime|null
139
     */
140 4
    public function getLastRun()
141
    {
142 4
        return $this->last_run ? clone $this->last_run : null;
143
    }
144
145
    /**
146
     * @param \DateTime $next_run
147
     *
148
     * @return Task
149
     */
150 2
    public function setNextRun(\DateTime $next_run)
151
    {
152 2
        $this->next_run = clone $next_run;
153 2
        return $this;
154
    }
155
156
    /**
157
     * @return \DateTime
158
     */
159 2
    public function getNextRun()
160
    {
161 2
        return clone $this->next_run;
162
    }
163
164
    /**
165
     * @param int $interval
166
     *
167
     * @return Task
168
     */
169 3
    public function setInterval($interval)
170
    {
171 3
        if ($interval > 0) {
172 1
            $this->setModify(sprintf('+%s second', (int)$interval));
173
        }
174 3
        return $this;
175
    }
176
177
    /**
178
     * @param string $modify
179
     *
180
     * @return Task
181
     */
182 5
    public function setModify($modify)
183
    {
184 5
        $this->modify = $modify;
185 5
        return $this;
186
    }
187
188
    /**
189
     * @return string
190
     */
191 9
    public function getModify()
192
    {
193 9
        return $this->modify;
194
    }
195
196
    /**
197
     * @param int $status
198
     *
199
     * @return Task
200
     */
201 4
    public function setStatus($status)
202
    {
203 4
        $this->status = $status;
204 4
        return $this;
205
    }
206
207
    /**
208
     * @return int
209
     */
210 4
    public function getStatus()
211
    {
212 4
        return $this->status;
213
    }
214
215
    /**
216
     * @return int[]
217
     */
218 1
    public static function getStatuses()
219
    {
220
        return [
221 1
            self::STATUS_DISABLED,
222 1
            self::STATUS_ENABLED
223
        ];
224
    }
225
226
    /**
227
     * @param ExecutionContextInterface $context
228
     */
229 2
    public function isModifyValid(ExecutionContextInterface $context)
230
    {
231 2
        if ($this->getModify() && strtotime($this->getModify()) === false) {
232
            $context
233 1
                ->buildViolation('Wrong date/time format')
234 1
                ->atPath('modify')
235 1
                ->addViolation();
236
        }
237 2
    }
238
239
    /**
240
     * Update task after execution
241
     */
242 3
    public function executed()
243
    {
244 3
        $this->setLastRun(new \DateTime());
245 3
        if (!$this->getModify()) {
246 2
            $this->setStatus(self::STATUS_DISABLED);
247
        }
248 3
        if ($this->getStatus() == self::STATUS_ENABLED) {
249
            // find near time task launch
250 1
            $next_run = $this->getNextRun();
251
            do {
252
                // failed to compute time of next run
253 1
                if ($next_run->modify($this->getModify()) === false) {
254
                    $this->setModify('');
255
                    $this->setStatus(self::STATUS_DISABLED);
256
                    break;
257
                }
258 1
            } while ($next_run->getTimestamp() <= time());
259 1
            $this->setNextRun($next_run);
260
        }
261 3
    }
262
}
263