Completed
Push — 0.4.5 ( f33756...7e44db )
by Peter
05:07 queued 02:56
created

Task::setStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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