Message::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Model;
15
16
class Message implements MessageInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $type;
22
23
    /**
24
     * @var array
25
     */
26
    protected $body;
27
28
    /**
29
     * @var int
30
     */
31
    protected $state;
32
33
    /**
34
     * @var int
35
     */
36
    protected $restartCount = 0;
37
38
    /**
39
     * @var string
40
     */
41
    protected $group;
42
43
    /**
44
     * @var \DateTime
45
     */
46
    protected $createdAt;
47
48
    /**
49
     * @var \DateTime
50
     */
51
    protected $updatedAt;
52
53
    /**
54
     * @var \DateTime
55
     */
56
    protected $startedAt;
57
58
    /**
59
     * @var \DateTime
60
     */
61
    protected $completedAt;
62
63
    public function __construct()
64
    {
65
        $this->createdAt = new \DateTime();
66
        $this->updatedAt = new \DateTime();
67
        $this->state = self::STATE_OPEN;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function __clone()
74
    {
75
        $this->state = self::STATE_OPEN;
76
        $this->startedAt = null;
77
        $this->completedAt = null;
78
79
        $this->createdAt = new \DateTime();
80
        $this->updatedAt = new \DateTime();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function setBody(array $body): void
87
    {
88
        $this->body = $body;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getBody()
95
    {
96
        return $this->body;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getValue($names, $default = null)
103
    {
104
        if (!\is_array($names)) {
105
            $names = [$names];
106
        }
107
108
        $body = $this->getBody();
109
        foreach ($names as $name) {
110
            if (!isset($body[$name])) {
111
                return $default;
112
            }
113
114
            $body = $body[$name];
115
        }
116
117
        return $body;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function setCompletedAt(?\DateTime $completedAt = null): void
124
    {
125
        $this->completedAt = $completedAt;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getCompletedAt()
132
    {
133
        return $this->completedAt;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function setCreatedAt(?\DateTime $createdAt = null): void
140
    {
141
        $this->createdAt = $createdAt;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function getCreatedAt()
148
    {
149
        return $this->createdAt;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function setGroup($group): void
156
    {
157
        $this->group = $group;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function getGroup()
164
    {
165
        return $this->group;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function setType($type): void
172
    {
173
        $this->type = $type;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function getType()
180
    {
181
        return $this->type;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function setState($state): void
188
    {
189
        $this->state = $state;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function getState()
196
    {
197
        return $this->state;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function setRestartCount($restartCount): void
204
    {
205
        $this->restartCount = $restartCount;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function getRestartCount()
212
    {
213
        return $this->restartCount;
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function setUpdatedAt(?\DateTime $updatedAt = null): void
220
    {
221
        $this->updatedAt = $updatedAt;
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function getUpdatedAt()
228
    {
229
        return $this->updatedAt;
230
    }
231
232
    /**
233
     * @return string[]
234
     */
235
    public static function getStateList()
236
    {
237
        return [
238
            self::STATE_OPEN => 'open',
239
            self::STATE_IN_PROGRESS => 'in_progress',
240
            self::STATE_DONE => 'done',
241
            self::STATE_ERROR => 'error',
242
            self::STATE_CANCELLED => 'cancelled',
243
        ];
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249
    public function setStartedAt(?\DateTime $startedAt = null): void
250
    {
251
        $this->startedAt = $startedAt;
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function getStartedAt()
258
    {
259
        return $this->startedAt;
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    public function getStateName()
266
    {
267
        $list = self::getStateList();
268
269
        return isset($list[$this->getState()]) ? $list[$this->getState()] : '';
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function isRunning()
276
    {
277
        return self::STATE_IN_PROGRESS === $this->state;
278
    }
279
280
    /**
281
     * {@inheritdoc}
282
     */
283
    public function isCancelled()
284
    {
285
        return self::STATE_CANCELLED === $this->state;
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291
    public function isError()
292
    {
293
        return self::STATE_ERROR === $this->state;
294
    }
295
296
    /**
297
     * {@inheritdoc}
298
     */
299
    public function isOpen()
300
    {
301
        return self::STATE_OPEN === $this->state;
302
    }
303
}
304