Passed
Pull Request — master (#778)
by Matias
08:57 queued 04:20
created

Image::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 2
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017-2018, 2020-2021 Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]>
5
 *
6
 * @author Branko Kokanovic <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\FaceRecognition\Db;
25
26
use JsonSerializable;
27
28
use OCP\AppFramework\Db\Entity;
29
30
/**
31
 * Image represent one image file for one user.
32
 *
33
 * @method string getUser()
34
 * @method void setUser(string $user)
35
 *
36
 * @method integer getFile()
37
 * @method void setFile(integer $file)
38
 *
39
 * @method integer getModel()
40
 * @method void setModel(integer $model)
41
 *
42
 * @method string|null getError()
43
 * @method void setError(string $error)
44
 *
45
 * @method bool getIsProcessed()
46
 * @method void setIsProcessed($isProcessed)
47
 *
48
 * @method void setLastProcessedTime($lastProcessedTime)
49
 *
50
 * @method void setProcessingDuration(int $processingDuration)
51
 *
52
 */
53
class Image extends Entity implements JsonSerializable {
54
55
	/**
56
	 * User this image belongs to.
57
	 *
58
	 * @var string
59
	 * */
60
	public $user;
61
62
	/**
63
	 * File that this image refer to.
64
	 * todo: add proper getters in whole class
65
	 *
66
	 * @var integer
67
	 */
68
	public $file;
69
70
	/**
71
	 * Face model that processed this image.
72
	 *
73
	 * @var integer
74
	 */
75
	protected $model;
76
77
	/**
78
	 * Whether this image is processed or not. Needed because image doesn't have to have any faces on it,
79
	 * yet we still need to know if it is being processed or not.
80
	 *
81
	 * @var bool
82
	 */
83
	protected $isProcessed;
84
85
	/**
86
	 * Description of error that happened during image processing.
87
	 * If it exist, image processing should be skipped even if $is_processed is false.
88
	 *
89
	 * @var string|null
90
	 */
91
	protected $error;
92
93
	/**
94
	 * Timestamp when this image was last processed.
95
	 *
96
	 * @var \DateTime|null
97
	 */
98
	protected $lastProcessedTime;
99
100
	/**
101
	 * Duration (in ms) it took to completely process this image. Should serve as a way to give estimates to user.
102
	 *
103
	 * @var integer|null
104
	*/
105
	protected $processingDuration;
106
107 23
	public function __construct() {
108 23
		$this->addType('id', 'integer');
109 23
		$this->addType('user', 'string');
110 23
		$this->addType('file', 'integer');
111 23
		$this->addType('model', 'integer');
112 23
		$this->addType('isProcessed', 'bool');
113
	}
114
115
	public function jsonSerialize() {
116
		return [
117
			'id' => $this->id,
118
			'user' => $this->user,
119
			'file' => $this->file,
120
			'model' => $this->model,
121
			'is_processed' => $this->isProcessed,
122
			'error' => $this->error,
123
			'last_processed_time' => $this->lastProcessedTime,
124
			'processing_duration' => $this->processingDuration
125
		];
126
	}
127
128
	public function setIsProcessed($isProcessed): void {
129
		if (is_bool($isProcessed)) {
130
			$this->isProcessed = $isProcessed;
131
		} else {
132
			$this->isProcessed = filter_var($isProcessed, FILTER_VALIDATE_BOOLEAN);
133
		}
134
		$this->markFieldUpdated('isProcessed');
135
	}
136
137
}
138