Passed
Push — master ( f7c888...c834f4 )
by Jim
01:11
created

TaskEvent::setErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace Jarobe\TaskRunnerBundle\Entity;
5
6
use Doctrine\ORM\Mapping as ORM;
7
use Jarobe\TaskRunnerBundle\TaskType\TaskTypeInterface;
8
9
/**
10
 * @ORM\Table(name="task_event")
11
 * @ORM\Entity(repositoryClass="Jarobe\TaskRunnerBundle\Repository\TaskEventRepository")
12
 */
13
class TaskEvent implements TaskEventInterface
14
{
15
    /**
16
     * @var integer
17
     *
18
     * @ORM\Column(name="id", type="integer")
19
     * @ORM\Id
20
     * @ORM\GeneratedValue(strategy="AUTO")
21
     */
22
    private $id;
23
24
    /**
25
     * @var string
26
     *
27
     * @ORM\Column(name="task_name", type="string", length=100)
28
     */
29
    private $taskName;
30
31
32
    /**
33
     * Many of the Tasks that are run have a specified time that they run for. Because of this, we store the TargetTime
34
     * outside of the payload. This helps significantly with querying what commands have been run for a date, as well.
35
     *
36
     * @var \DateTime
37
     *
38
     * @ORM\Column(name="target_time", type="datetime", nullable=true)
39
     */
40
    private $targetTime;
41
42
    /**
43
     * @var array
44
     *
45
     * @ORM\Column(name="payload", type="array")
46
     */
47
    private $payload;
48
49
    /**
50
     * @var \DateTime
51
     *
52
     * @ORM\Column(name="initiated_at", type="datetime", nullable=true)
53
     */
54
    private $initiatedAt;
55
56
    /**
57
     * @var \DateTime
58
     *
59
     * @ORM\Column(name="failed_at", type="datetime", nullable=true)
60
     */
61
    private $failedAt;
62
63
    /**
64
     * @var \DateTime
65
     *
66
     * @ORM\Column(name="completed_at", type="datetime", nullable=true)
67
     */
68
    private $completedAt;
69
70
    /**
71
     * @var array|null
72
     *
73
     * @ORM\Column(name="errors", type="json_array", nullable=true)
74
     */
75
    private $errors;
76
77
    /**
78
     * @return int
79
     */
80
    public function getId()
81
    {
82
        return $this->id;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 1
    public function getTaskName()
89
    {
90 1
        return $this->taskName;
91
    }
92
93
    /**
94
     * @param string $taskName
95
     * @return TaskEvent
96
     */
97 1
    public function setTaskName($taskName)
98
    {
99 1
        $this->taskName = $taskName;
100 1
        return $this;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getPayload()
107
    {
108
        return $this->payload;
109
    }
110
111
    /**
112
     * @param array $payload
113
     * @return TaskEvent
114
     */
115 1
    public function setPayload($payload)
116
    {
117 1
        $this->payload = $payload;
118 1
        return $this;
119
    }
120
121
    /**
122
     * @return \DateTime
123
     */
124 1
    public function getInitiatedAt()
125
    {
126 1
        return $this->initiatedAt;
127
    }
128
129
    /**
130
     * @param \DateTime $initiatedAt
131
     * @return TaskEvent
132
     */
133 1
    public function setInitiatedAt(\DateTime $initiatedAt)
134
    {
135 1
        $this->initiatedAt = $initiatedAt;
136 1
        return $this;
137
    }
138
139
    /**
140
     * @return \DateTime
141
     */
142 2
    public function getCompletedAt()
143
    {
144 2
        return $this->completedAt;
145
    }
146
147
    /**
148
     * @param \DateTime $completedAt
149
     * @return TaskEvent
150
     */
151 2
    public function setCompletedAt(\DateTime $completedAt)
152
    {
153 2
        $this->completedAt = $completedAt;
154 2
        return $this;
155
    }
156
157
    /**
158
     * @return \DateTime
159
     */
160 2
    public function getFailedAt()
161
    {
162 2
        return $this->failedAt;
163
    }
164
165
    /**
166
     * @param $failedAt
167
     * @return $this
168
     */
169 2
    public function setFailedAt(\DateTime $failedAt)
170
    {
171 2
        $this->failedAt = $failedAt;
172 2
        return $this;
173
    }
174
175
    /**
176
     * @return array
177
     */
178 4
    public function getErrors()
179
    {
180 4
        return $this->errors;
181
    }
182
183
    /**
184
     * @param array|null $errors
185
     * @return $this
186
     */
187 2
    public function setErrors(array $errors = null)
188
    {
189 2
        $this->errors = $errors;
190
191 2
        return $this;
192
    }
193
194
    /**
195
     * @return \DateTime
196
     */
197 1
    public function getTargetTime()
198
    {
199 1
        return $this->targetTime;
200
    }
201
202
    /**
203
     * @param \DateTime $targetTime
204
     * @return $this
205
     */
206 1
    public function setTargetTime($targetTime = null)
207
    {
208 1
        $this->targetTime = $targetTime;
209
210 1
        return $this;
211
    }
212
213
    /**
214
     * @return TaskEventInterface
215
     */
216 1
    public function initiate()
217
    {
218 1
        $this->setInitiatedAt(new \DateTime());
219 1
    }
220
221
    /**
222
     * @return TaskEventInterface
223
     */
224 2
    public function setAsCompleted()
225
    {
226 2
        $this->setCompletedAt(new \DateTime());
227 2
    }
228
229
    /**
230
     * @param array $errors
231
     */
232 2
    public function setAsFailed(array $errors)
233
    {
234 2
        $this->setFailedAt(new \DateTime())
235 2
            ->setErrors($errors)
236
        ;
237 2
    }
238
239
    /**
240
     * @param $name
241
     * @param TaskTypeInterface $taskType
242
     * @return TaskEventInterface
243
     */
244 1
    public function intialize($name, TaskTypeInterface $taskType)
245
    {
246 1
        $this->setTaskName($name)
247 1
            ->setPayload($taskType->getPayload())
248 1
            ->setTargetTime($taskType->getTargetTime())
249
        ;
250 1
    }
251
252
    /** @return bool */
253
    public function isComplete()
254
    {
255
        return $this->completedAt !== null;
256
    }
257
258
    /** @return bool */
259
    public function isFailed()
260
    {
261
        return $this->failedAt !== null;
262
    }
263
}
264