Passed
Pull Request — master (#751)
by Matias
07:33 queued 04:51
created

ModelManager::getAllModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2020, Matias De lellis <[email protected]>
4
 *
5
 * @author Matias De lellis <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\FaceRecognition\Model;
25
26
use OCP\IUser;
27
use OCP\IUserManager;
28
29
use OCA\FaceRecognition\Model\IModel;
30
31
use OCA\FaceRecognition\Service\SettingsService;
32
33
use OCA\FaceRecognition\Model\DlibCnnModel\DlibCnn68Model;
34
use OCA\FaceRecognition\Model\DlibCnnModel\DlibCnn5Model;
35
36
use OCA\FaceRecognition\Model\DlibHogModel\DlibHogModel;
37
38
use OCA\FaceRecognition\Model\DlibCnnHogModel\DlibCnnHogModel;
39
use OCA\FaceRecognition\Model\DlibTaguchiHogModel\DlibTaguchiHogModel;
40
41
use OCA\FaceRecognition\Model\ExternalModel\ExternalModel;
42
43
class ModelManager {
44
45
	/** There is no default model. This is used by tests */
46
	const DEFAULT_FACE_MODEL_ID = 1;
47
48
	/** @var IUserManager */
49
	private $userManager;
50
51
	/** @var SettingsService */
52
	private $settingsService;
53
54
	/** @var DlibCnn5Model */
55
	private $dlibCnn5Model;
56
57
	/** @var DlibCnn68Model */
58
	private $dlibCnn68Model;
59
60
	/** @var DlibHogModel */
61
	private $dlibHogModel;
62
63
	/** @var DlibCnnHogModel */
64
	private $dlibCnnHogModel;
65
66
	/** @var ExternalModel */
67
	private $externalModel;
68
69
	/** @var DlibTaguchiHogModel */
70
	private $dlibTaguchiHogModel;
71
72
	/**
73
	 * @patam IUserManager $userManager
74
	 * @param SettingsService $settingsService
75
	 * @param DlibCnn5Model $dlibCnn5Model
76
	 * @param DlibCnn68Model $dlibCnn68Model
77
	 * @param DlibHogModel $dlibHogModel
78
	 * @param DlibCnnHogModel $dlibCnnHogModel
79
	 * @param ExternalModel $externalModel
80
	 * @param DlibTaguchiHogModel $dlibTaguchiHogModel
81
	 */
82 1
	public function __construct(IUserManager    $userManager,
83
	                            SettingsService $settingsService,
84
	                            DlibCnn5Model   $dlibCnn5Model,
85
	                            DlibCnn68Model  $dlibCnn68Model,
86
	                            DlibHogModel    $dlibHogModel,
87
	                            DlibCnnHogModel $dlibCnnHogModel,
88
	                            ExternalModel   $externalModel,
89
	                            DlibTaguchiHogModel $dlibTaguchiHogModel)
90
	{
91 1
		$this->userManager     = $userManager;
92 1
		$this->settingsService = $settingsService;
93
94 1
		$this->dlibCnn5Model   = $dlibCnn5Model;
95 1
		$this->dlibCnn68Model  = $dlibCnn68Model;
96 1
		$this->dlibHogModel    = $dlibHogModel;
97 1
		$this->dlibCnnHogModel = $dlibCnnHogModel;
98 1
		$this->externalModel   = $externalModel;
99 1
		$this->dlibTaguchiHogModel = $dlibTaguchiHogModel;
100
	}
101
102
	/**
103
	 * @param int $version model version
104
	 * @return IModel|null
105
	 */
106 4
	public function getModel(int $version): ?IModel {
107
		switch ($version) {
108 4
			case DlibCnn5Model::FACE_MODEL_ID:
109 4
				$model = $this->dlibCnn5Model;
110 4
				break;
111
			case DlibCnn68Model::FACE_MODEL_ID:
112
				$model = $this->dlibCnn68Model;
113
				break;
114
			case DlibHogModel::FACE_MODEL_ID:
115
				$model = $this->dlibHogModel;
116
				break;
117
			case DlibCnnHogModel::FACE_MODEL_ID:
118
				$model = $this->dlibCnnHogModel;
119
				break;
120
			case ExternalModel::FACE_MODEL_ID:
121
				$model = $this->externalModel;
122
				break;
123
			case DlibTaguchiHogModel::FACE_MODEL_ID:
124
				$model = $this->dlibTaguchiHogModel;
125
				break;
126
			default:
127
				$model = null;
128
				break;
129
		}
130 4
		return $model;
131
	}
132
133
	/**
134
	 * @return IModel|null
135
	 */
136 4
	public function getCurrentModel(): ?IModel {
137 4
		$modelId = $this->settingsService->getCurrentFaceModel();
138 4
		return $this->getModel($modelId);
139
	}
140
141
	/**
142
	 * @return IModel[]
143
	 */
144
	public function getAllModels(): array {
145
		return [
146
			$this->dlibCnn5Model,
147
			$this->dlibCnn68Model,
148
			$this->dlibHogModel,
149
			$this->dlibCnnHogModel,
150
			$this->externalModel,
151
			$this->dlibTaguchiHogModel
152
		];
153
	}
154
155
	/**
156
	 * Set default model to use
157
	 * @param int $version model version
158
	 * @return bool true if successful. False otherwise
159
	 */
160
	public function setDefault(int $version): bool {
161
		$error_message = '';
162
		$model = $this->getModel($version);
163
		if (is_null($model) || !$model->isInstalled() || !$model->meetDependencies($error_message))
164
			return false;
165
166
		if ($this->settingsService->getCurrentFaceModel() !== $model->getId()) {
167
			$this->settingsService->setCurrentFaceModel($model->getId());
168
		}
169
		$this->userManager->callForAllUsers(function (IUser $user) {
170
			$this->settingsService->setUserFullScanDone(false, $user->getUID());
171
		});
172
173
		return true;
174
	}
175
176
}