Passed
Push — settings-service ( 3fd917...8895a7 )
by Matias
03:54
created

FaceManagementService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 60
ccs 0
cts 30
cp 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resetAllForUser() 0 5 1
A resetAll() 0 12 3
A __construct() 0 11 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017, 2020 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\Service;
25
26
use OCP\IUser;
27
use OCP\IUserManager;
28
29
use OCA\FaceRecognition\Db\FaceMapper;
30
use OCA\FaceRecognition\Db\ImageMapper;
31
use OCA\FaceRecognition\Db\PersonMapper;
32
33
use OCA\FaceRecognition\Service\SettingService;
34
35
/**
36
 * Background service. Both command and cron job are calling this service for long-running background operations.
37
 * Background processing for face recognition is comprised of several steps, called tasks. Each task is independent,
38
 * idempotent, DI-aware logic unit that yields. Since tasks are non-preemptive, they should yield from time to time, so we son't end up
39
 * working for more than given timeout.
40
 *
41
 * Tasks can be seen as normal sequential functions, but they are easier to work with,
42
 * reason about them and test them independently. Other than that, they are really glorified functions.
43
 */
44
class FaceManagementService {
45
46
	/** @var IUserManager */
47
	private $userManager;
48
49
	/** @var FaceMapper */
50
	private $faceMapper;
51
52
	/** @var ImageMapper */
53
	private $imageMapper;
54
55
	/** @var PersonMapper */
56
	private $personMapper;
57
58
	/** @var SettingService */
59
	private $settingService;
60
61
	public function __construct(IUserManager   $userManager,
62
	                            FaceMapper     $faceMapper,
63
	                            ImageMapper    $imageMapper,
64
	                            PersonMapper   $personMapper,
65
	                            SettingService $settingService)
66
	{
67
		$this->userManager    = $userManager;
68
		$this->faceMapper     = $faceMapper;
69
		$this->imageMapper    = $imageMapper;
70
		$this->personMapper   = $personMapper;
71
		$this->settingService = $settingService;
72
	}
73
74
	/**
75
	 * Deletes all faces, images and persons found. IF no user is given, resetting is executed for all users.
76
	 *
77
	 * @param IUser|null $user Optional user to execute resetting for
78
	 */
79
	public function resetAll(IUser $user = null) {
80
		$eligable_users = array();
81
		if (is_null($user)) {
82
			$this->userManager->callForAllUsers(function (IUser $user) use (&$eligable_users) {
83
				$eligable_users[] = $user->getUID();
84
			});
85
		} else {
86
			$eligable_users[] = $user->getUID();
87
		}
88
89
		foreach($eligable_users as $user) {
90
			$this->resetAllForUser($user);
91
		}
92
	}
93
94
	/**
95
	 * Deletes all faces, images and persons found for a given user.
96
	 *
97
	 * @param string $user ID of user to execute resetting for
98
	 */
99
	public function resetAllForUser(string $userId) {
100
		$this->faceMapper->deleteUserFaces($userId);
101
		$this->personMapper->deleteUserPersons($userId);
102
		$this->imageMapper->deleteUserImages($userId);
103
		$this->settingService->setUserFullScanDone(false, $userId);
104
	}
105
106
}