Passed
Pull Request — master (#313)
by Matias
07:10 queued 05:26
created

ModelManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 39.53%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 111
ccs 17
cts 43
cp 0.3953
rs 10
c 5
b 0
f 0
wmc 13

5 Methods

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