Completed
Push — master ( 8528a2...610c9c )
by C
05:11
created

Download::setHash()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace Tartana\Entity;
3
use Doctrine\ORM\Mapping as ORM;
4
5
/**
6
 * @ORM\Entity
7
 * @ORM\Table(name="tartana_download",
8
 * indexes={@ORM\Index(name="destination_idx", columns={"destination"})})
9
 */
10
class Download extends Base
11
{
12
13
	const STATE_DOWNLOADING_NOT_STARTED = 1;
14
15
	const STATE_DOWNLOADING_STARTED = 2;
16
17
	const STATE_DOWNLOADING_COMPLETED = 3;
18
19
	const STATE_DOWNLOADING_ERROR = 4;
20
21
	const STATE_PROCESSING_NOT_STARTED = 5;
22
23
	const STATE_PROCESSING_STARTED = 6;
24
25
	const STATE_PROCESSING_COMPLETED = 7;
26
27
	const STATE_PROCESSING_ERROR = 8;
28
29
	public static $STATES_ALL = [
30
			self::STATE_DOWNLOADING_NOT_STARTED,
31
			self::STATE_DOWNLOADING_STARTED,
32
			self::STATE_DOWNLOADING_COMPLETED,
33
			self::STATE_DOWNLOADING_ERROR,
34
			self::STATE_PROCESSING_NOT_STARTED,
35
			self::STATE_PROCESSING_STARTED,
36
			self::STATE_PROCESSING_COMPLETED,
37
			self::STATE_PROCESSING_ERROR
38
	];
39
40
	/**
41
	 * @ORM\Column(type="integer")
42
	 * @ORM\Id
43
	 * @ORM\GeneratedValue(strategy="AUTO")
44
	 */
45
	protected $id;
46
47
	/**
48
	 * @ORM\Column(type="string", length=255, unique=true)
49
	 */
50
	protected $link;
51
52
	/**
53
	 * @ORM\Column(type="string", length=255)
54
	 */
55
	protected $destination;
56
57
	/**
58
	 * @ORM\Column(type="string", length=255, nullable=true)
59
	 */
60
	protected $file_name;
61
62
	/**
63
	 * @ORM\Column(type="decimal", scale=2, nullable=true)
64
	 */
65
	protected $progress = 0.00;
66
67
	/**
68
	 * @ORM\Column(type="smallint")
69
	 */
70
	protected $state = self::STATE_DOWNLOADING_NOT_STARTED;
71
72
	/**
73
	 * @ORM\Column(type="datetime", nullable=true)
74
	 */
75
	protected $started_at;
76
77
	/**
78
	 * @ORM\Column(type="datetime", nullable=true)
79
	 */
80
	protected $finished_at;
81
82
	/**
83
	 * @ORM\Column(type="integer", nullable=true)
84
	 */
85
	protected $size = 0;
86
87
	/**
88
	 * @ORM\Column(type="integer", nullable=true)
89
	 */
90
	protected $pid = 0;
91
92
	/**
93
	 * @ORM\Column(type="string", length=255, nullable=true)
94
	 */
95
	protected $message;
96
97
	/**
98
	 * @ORM\Column(type="string", length=255, nullable=true)
99
	 */
100
	protected $hash;
101
102
	/**
103
	 * Get id
104
	 *
105
	 * @return integer
106
	 */
107 62
	public function getId ()
108
	{
109 62
		return $this->id;
110
	}
111
112
	/**
113
	 * Set id
114
	 *
115
	 * @return Download
116
	 */
117 25
	public function setId ($id)
118
	{
119 25
		$this->id = $id;
120
121 25
		return $this;
122
	}
123
124
	/**
125
	 * Set link
126
	 *
127
	 * @param string $link
128
	 *
129
	 * @return Download
130
	 */
131 133
	public function setLink ($link)
132
	{
133 133
		$this->link = $link;
134
135 133
		return $this;
136
	}
137
138
	/**
139
	 * Get link
140
	 *
141
	 * @return string
142
	 */
143 101
	public function getLink ()
144
	{
145 101
		return $this->link;
146
	}
147
148
	/**
149
	 * Set progress, if allow reset is set, then a progress smaller than the
150
	 * actual one can be set.
151
	 *
152
	 * @param string $progress
153
	 * @param boolean $allowReset
154
	 *
155
	 * @return Download
156
	 */
157 49
	public function setProgress ($progress, $allowReset = false)
158
	{
159 49
		$progress = (float) $progress;
160 49
		if ($progress < 0)
161
		{
162 1
			$progress = 0;
163
		}
164 49
		if ($progress > 100)
165
		{
166 1
			$progress = 100;
167
		}
168 49
		if ($this->progress > $progress && ! $allowReset)
169
		{
170 1
			return $this;
171
		}
172 49
		$this->progress = number_format($progress, 2);
173
174 49
		return $this;
175
	}
176
177
	/**
178
	 * Get progress
179
	 *
180
	 * @return string
181
	 */
182 12
	public function getProgress ()
183
	{
184 12
		return $this->progress;
185
	}
186
187
	/**
188
	 * Set progress, if allow reset is set, then a progress smaller than the
189
	 * actual one can be set.
190
	 *
191
	 * @param string $size
192
	 *
193
	 * @return Download
194
	 */
195 11
	public function setSize ($size)
196
	{
197 11
		$this->size = (int) $size;
198
199 11
		return $this;
200
	}
201
202
	/**
203
	 * Get size
204
	 *
205
	 * @return integer
206
	 */
207 12
	public function getSize ()
208
	{
209 12
		return $this->size;
210
	}
211
212
	/**
213
	 * Set state
214
	 *
215
	 * @param integer $state
216
	 *
217
	 * @return Download
218
	 * @see Download::STATE_DOWNLOADING_NOT_STARTED
219
	 * @see Download::STATE_DOWNLOADING_STARTED
220
	 * @see Download::STATE_DOWNLOADING_COMPLETED
221
	 * @see Download::STATE_DOWNLOADING_ERROR
222
	 * @see Download::STATE_PROCESSING_NOT_STARTED
223
	 * @see Download::STATE_PROCESSING_STARTED
224
	 * @see Download::STATE_PROCESSING_COMPLETED
225
	 * @see Download::STATE_PROCESSING_ERROR
226
	 */
227 127
	public function setState ($state)
228
	{
229
		// Check if it is a valid state
230 127
		if (! in_array($state,
231
				[
232 127
						self::STATE_DOWNLOADING_NOT_STARTED,
233 127
						self::STATE_DOWNLOADING_STARTED,
234 127
						self::STATE_DOWNLOADING_COMPLETED,
235 127
						self::STATE_DOWNLOADING_ERROR,
236 127
						self::STATE_PROCESSING_NOT_STARTED,
237 127
						self::STATE_PROCESSING_STARTED,
238 127
						self::STATE_PROCESSING_COMPLETED,
239 127
						self::STATE_PROCESSING_ERROR
240
				]))
241
		{
242 1
			return $this;
243
		}
244 126
		$this->state = $state;
245
246 126
		return $this;
247
	}
248
249
	/**
250
	 * Get state
251
	 *
252
	 * @return integer
253
	 */
254 116
	public function getState ()
255
	{
256 116
		return $this->state;
257
	}
258
259
	/**
260
	 * Set startedAt
261
	 *
262
	 * @param \DateTime $startedAt
263
	 *
264
	 * @return Download
265
	 */
266 14
	public function setStartedAt (\DateTime $startedAt = null)
267
	{
268 14
		$this->started_at = $startedAt;
269
270 14
		return $this;
271
	}
272
273
	/**
274
	 * Get startedAt
275
	 *
276
	 * @return \DateTime
277
	 */
278 2
	public function getStartedAt ()
279
	{
280 2
		return $this->started_at;
281
	}
282
283
	/**
284
	 * Set finishedAt
285
	 *
286
	 * @param \DateTime $finishedAt
287
	 *
288
	 * @return Download
289
	 */
290 63
	public function setFinishedAt (\DateTime $finishedAt = null)
291
	{
292 63
		$this->finished_at = $finishedAt;
293
294 63
		return $this;
295
	}
296
297
	/**
298
	 * Get finishedAt
299
	 *
300
	 * @return \DateTime
301
	 */
302 2
	public function getFinishedAt ()
303
	{
304 2
		return $this->finished_at;
305
	}
306
307
	/**
308
	 * Set message
309
	 *
310
	 * @param string $message
311
	 *
312
	 * @return Download
313
	 */
314 67
	public function setMessage ($message)
315
	{
316 67
		$this->message = $message;
317
318 67
		return $this;
319
	}
320
321
	/**
322
	 * Get message
323
	 *
324
	 * @return string
325
	 */
326 82
	public function getMessage ()
327
	{
328 82
		return $this->message;
329
	}
330
331
	/**
332
	 * Set destination
333
	 *
334
	 * @param string $destination
335
	 *
336
	 * @return Download
337
	 */
338 151
	public function setDestination ($destination)
339
	{
340 151
		$this->destination = $destination;
341
342 151
		return $this;
343
	}
344
345
	/**
346
	 * Get destination
347
	 *
348
	 * @return string
349
	 */
350 105
	public function getDestination ()
351
	{
352 105
		return $this->destination;
353
	}
354
355
	/**
356
	 * Set fileName
357
	 *
358
	 * @param string $fileName
359
	 *
360
	 * @return Download
361
	 */
362 49
	public function setFileName ($fileName)
363
	{
364 49
		$this->file_name = $fileName;
365
366 49
		return $this;
367
	}
368
369
	/**
370
	 * Get fileName
371
	 *
372
	 * @return string
373
	 */
374 70
	public function getFileName ()
375
	{
376 70
		return $this->file_name;
377
	}
378
379
	/**
380
	 * Set pid
381
	 *
382
	 * @param integer $pid
383
	 *
384
	 * @return Download
385
	 */
386 17
	public function setPid ($pid)
387
	{
388 17
		$this->pid = $pid;
389
390 17
		return $this;
391
	}
392
393
	/**
394
	 * Get pid
395
	 *
396
	 * @return integer
397
	 */
398 7
	public function getPid ()
399
	{
400 7
		return $this->pid;
401
	}
402
403
	/**
404
	 * Set hash
405
	 *
406
	 * @param string $hash
407
	 *
408
	 * @return Download
409
	 */
410 12
	public function setHash ($hash)
411
	{
412 12
		$this->hash = $hash;
413
414 12
		return $this;
415
	}
416
417
	/**
418
	 * Get hash
419
	 *
420
	 * @return string
421
	 */
422 35
	public function getHash ()
423
	{
424 35
		return $this->hash;
425
	}
426
427
	/**
428
	 * Resets the given download to a not started state including the dependant
429
	 * settings.
430
	 *
431
	 * @param Download $download
432
	 * @return Download
433
	 * @see Download::STATE_DOWNLOADING_NOT_STARTED
434
	 */
435 14
	public static function reset (Download $download)
436
	{
437 14
		$download->setState(Download::STATE_DOWNLOADING_NOT_STARTED);
438 14
		$download->setProgress(0, true);
439 14
		$download->setMessage(null);
440 14
		$download->setPid(0);
441 14
		$download->setStartedAt(null);
442 14
		$download->setFinishedAt(null);
443
444 14
		return $download;
445
	}
446
}
447