Passed
Push — master ( 0059ba...f54c15 )
by Matias
18:33 queued 04:55
created

ModelManager::getAllModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 0
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
class ModelManager {
39
40
	/** There is no default model. This is used by tests */
41
	const DEFAULT_FACE_MODEL_ID = 1;
42
43
	/** @var IUserManager */
44
	private $userManager;
45
46
	/** @var SettingsService */
47
	private $settingsService;
48
49
	/** @var DlibCnn5Model */
50
	private $dlibCnn5Model;
51
52
	/** @var DlibCnn68Model */
53
	private $dlibCnn68Model;
54
55
	/** @var DlibHogModel */
56
	private $dlibHogModel;
57
58
	/**
59
	 * @patam IUserManager $userManager
60
	 * @param SettingsService $settingsService
61
	 * @param DlibCnn5Model $dlibCnn5Model
62
	 * @param DlibCnn68Model $dlibCnn68Model
63
	 * @param DlibHogModel $dlibHogModel
64
	 */
65 1
	public function __construct(IUserManager    $userManager,
66
	                            SettingsService $settingsService,
67
	                            DlibCnn5Model   $dlibCnn5Model,
68
	                            DlibCnn68Model  $dlibCnn68Model,
69
	                            DlibHogModel    $dlibHogModel)
70
	{
71 1
		$this->userManager     = $userManager;
72 1
		$this->settingsService = $settingsService;
73
74 1
		$this->dlibCnn5Model   = $dlibCnn5Model;
75 1
		$this->dlibCnn68Model  = $dlibCnn68Model;
76 1
		$this->dlibHogModel    = $dlibHogModel;
77 1
	}
78
79
	/**
80
	 * @param int $version model version
81
	 * @return IModel|null
82
	 */
83
	public function getModel(int $version): ?IModel {
84
		switch ($version) {
85
			case 1:
86
				$model = $this->dlibCnn5Model;
87
				break;
88
			case 2:
89
				$model = $this->dlibCnn68Model;
90
				break;
91
			case 3:
92
				$model = $this->dlibHogModel;
93
				break;
94
			default:
95
				$model = null;
96
				break;
97
		}
98
		return $model;
99
	}
100
101
	/**
102
	 * @return IModel|null
103
	 */
104
	public function getCurrentModel(): ?IModel {
105
		$modelId = $this->settingsService->getCurrentFaceModel();
106
		return $this->getModel($modelId);
107
	}
108
109
	/**
110
	 * @return IModel[]
111
	 */
112
	public function getAllModels(): array {
113
		return [
114
			$this->dlibCnn5Model,
115
			$this->dlibCnn68Model,
116
			$this->dlibHogModel
117
		];
118
	}
119
120
	/**
121
	 * Set default model to use
122
	 * @param int $version model version
123
	 * @return bool true if successful. False otherwise
124
	 */
125
	public function setDefault(int $version): bool {
126
		$error_message = '';
127
		$model = $this->getModel($version);
128
		if (is_null($model) || !$model->isInstalled() || !$model->meetDependencies($error_message))
129
			return false;
130
131
		if ($this->settingsService->getCurrentFaceModel() !== $model->getId()) {
132
			$this->settingsService->setCurrentFaceModel($model->getId());
133
		}
134
		$this->userManager->callForAllUsers(function (IUser $user) {
135
			$this->settingsService->setUserFullScanDone(false, $user->getUID());
136
		});
137
138
		return true;
139
	}
140
141
}