Passed
Push — dependabot/npm_and_yarn/ellipt... ( eb0fee )
by
unknown
24:49
created

ModelManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 36.17%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 53
c 6
b 0
f 0
dl 0
loc 121
ccs 17
cts 47
cp 0.3617
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllModels() 0 7 1
A setDefault() 0 14 5
A getCurrentModel() 0 3 1
A __construct() 0 16 1
B getModel() 0 22 6
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
40
use OCA\FaceRecognition\Model\ExternalModel\ExternalModel;
41
42
class ModelManager {
43
44
	/** There is no default model. This is used by tests */
45
	const DEFAULT_FACE_MODEL_ID = 1;
46
47
	/** @var IUserManager */
48
	private $userManager;
49
50
	/** @var SettingsService */
51
	private $settingsService;
52
53
	/** @var DlibCnn5Model */
54
	private $dlibCnn5Model;
55
56
	/** @var DlibCnn68Model */
57
	private $dlibCnn68Model;
58
59
	/** @var DlibHogModel */
60
	private $dlibHogModel;
61
62
	/** @var DlibCnnHogModel */
63
	private $dlibCnnHogModel;
64
65
	/** @var ExternalModel */
66
	private $externalModel;
67
68
	/**
69
	 * @patam IUserManager $userManager
70
	 * @param SettingsService $settingsService
71
	 * @param DlibCnn5Model $dlibCnn5Model
72
	 * @param DlibCnn68Model $dlibCnn68Model
73
	 * @param DlibHogModel $dlibHogModel
74
	 * @param DlibCnnHogModel $dlibCnnHogModel
75
	 * @param ExternalModel $externalModel
76
	 */
77 1
	public function __construct(IUserManager    $userManager,
78
	                            SettingsService $settingsService,
79
	                            DlibCnn5Model   $dlibCnn5Model,
80
	                            DlibCnn68Model  $dlibCnn68Model,
81
	                            DlibHogModel    $dlibHogModel,
82
	                            DlibCnnHogModel $dlibCnnHogModel,
83
	                            ExternalModel   $externalModel)
84
	{
85 1
		$this->userManager     = $userManager;
86 1
		$this->settingsService = $settingsService;
87
88 1
		$this->dlibCnn5Model   = $dlibCnn5Model;
89 1
		$this->dlibCnn68Model  = $dlibCnn68Model;
90 1
		$this->dlibHogModel    = $dlibHogModel;
91 1
		$this->dlibCnnHogModel = $dlibCnnHogModel;
92 1
		$this->externalModel   = $externalModel;
93 1
	}
94
95
	/**
96
	 * @param int $version model version
97
	 * @return IModel|null
98
	 */
99 4
	public function getModel(int $version): ?IModel {
100
		switch ($version) {
101 4
			case DlibCnn5Model::FACE_MODEL_ID:
102 4
				$model = $this->dlibCnn5Model;
103 4
				break;
104
			case DlibCnn68Model::FACE_MODEL_ID:
105
				$model = $this->dlibCnn68Model;
106
				break;
107
			case DlibHogModel::FACE_MODEL_ID:
108
				$model = $this->dlibHogModel;
109
				break;
110
			case DlibCnnHogModel::FACE_MODEL_ID:
111
				$model = $this->dlibCnnHogModel;
112
				break;
113
			case ExternalModel::FACE_MODEL_ID:
114
				$model = $this->externalModel;
115
				break;
116
			default:
117
				$model = null;
118
				break;
119
		}
120 4
		return $model;
121
	}
122
123
	/**
124
	 * @return IModel|null
125
	 */
126 4
	public function getCurrentModel(): ?IModel {
127 4
		$modelId = $this->settingsService->getCurrentFaceModel();
128 4
		return $this->getModel($modelId);
129
	}
130
131
	/**
132
	 * @return IModel[]
133
	 */
134
	public function getAllModels(): array {
135
		return [
136
			$this->dlibCnn5Model,
137
			$this->dlibCnn68Model,
138
			$this->dlibHogModel,
139
			$this->dlibCnnHogModel,
140
			$this->externalModel
141
		];
142
	}
143
144
	/**
145
	 * Set default model to use
146
	 * @param int $version model version
147
	 * @return bool true if successful. False otherwise
148
	 */
149
	public function setDefault(int $version): bool {
150
		$error_message = '';
151
		$model = $this->getModel($version);
152
		if (is_null($model) || !$model->isInstalled() || !$model->meetDependencies($error_message))
153
			return false;
154
155
		if ($this->settingsService->getCurrentFaceModel() !== $model->getId()) {
156
			$this->settingsService->setCurrentFaceModel($model->getId());
157
		}
158
		$this->userManager->callForAllUsers(function (IUser $user) {
159
			$this->settingsService->setUserFullScanDone(false, $user->getUID());
160
		});
161
162
		return true;
163
	}
164
165
}