Passed
Push — settings-service ( a793f8...527322 )
by Matias
04:01
created

FaceManagementService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 31.82%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 60
ccs 7
cts 22
cp 0.3182
rs 10
c 0
b 0
f 0
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) 2020, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2019, 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
25
namespace OCA\FaceRecognition\Service;
26
27
use OCP\IUser;
28
use OCP\IUserManager;
29
30
use OCA\FaceRecognition\Db\FaceMapper;
31
use OCA\FaceRecognition\Db\ImageMapper;
32
use OCA\FaceRecognition\Db\PersonMapper;
33
34
use OCA\FaceRecognition\Service\SettingsService;
35
36
/**
37
 * Background service. Both command and cron job are calling this service for long-running background operations.
38
 * Background processing for face recognition is comprised of several steps, called tasks. Each task is independent,
39
 * 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
40
 * working for more than given timeout.
41
 *
42
 * Tasks can be seen as normal sequential functions, but they are easier to work with,
43
 * reason about them and test them independently. Other than that, they are really glorified functions.
44
 */
45
class FaceManagementService {
46
47
	/** @var IUserManager */
48
	private $userManager;
49
50
	/** @var FaceMapper */
51
	private $faceMapper;
52
53
	/** @var ImageMapper */
54
	private $imageMapper;
55
56
	/** @var PersonMapper */
57
	private $personMapper;
58
59
	/** @var SettingsService */
60
	private $settingsService;
61
62 1
	public function __construct(IUserManager    $userManager,
63
	                            FaceMapper      $faceMapper,
64
	                            ImageMapper     $imageMapper,
65
	                            PersonMapper    $personMapper,
66
	                            SettingsService $settingsService)
67
	{
68 1
		$this->userManager     = $userManager;
69 1
		$this->faceMapper      = $faceMapper;
70 1
		$this->imageMapper     = $imageMapper;
71 1
		$this->personMapper    = $personMapper;
72 1
		$this->settingsService = $settingsService;
73 1
	}
74
75
	/**
76
	 * Deletes all faces, images and persons found. IF no user is given, resetting is executed for all users.
77
	 *
78
	 * @param IUser|null $user Optional user to execute resetting for
79
	 */
80
	public function resetAll(IUser $user = null) {
81
		$eligable_users = array();
82
		if (is_null($user)) {
83
			$this->userManager->callForAllUsers(function (IUser $user) use (&$eligable_users) {
84
				$eligable_users[] = $user->getUID();
85
			});
86
		} else {
87
			$eligable_users[] = $user->getUID();
88
		}
89
90
		foreach($eligable_users as $user) {
91
			$this->resetAllForUser($user);
92
		}
93
	}
94
95
	/**
96
	 * Deletes all faces, images and persons found for a given user.
97
	 *
98
	 * @param string $user ID of user to execute resetting for
99
	 */
100
	public function resetAllForUser(string $userId) {
101
		$this->faceMapper->deleteUserFaces($userId);
102
		$this->personMapper->deleteUserPersons($userId);
103
		$this->imageMapper->deleteUserImages($userId);
104
		$this->settingsService->setUserFullScanDone(false, $userId);
105
	}
106
107
}