Passed
Push — reset-functionality ( c3a340...c4d1b2 )
by Branko
02:25
created

FaceManagementService::resetAllForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017, 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;
25
26
use OCP\IConfig;
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\BackgroundJob\Tasks\AddMissingImagesTask;
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 IConfig */
48
	private $config;
49
50
	/** @var IUserManager */
51
	private $userManager;
52
53
	/** @var FaceMapper */
54
	private $faceMapper;
55
56
	/** @var ImageMapper */
57
	private $imageMapper;
58
59
	/** @var PersonMapper */
60
	private $personMapper;
61
62 1
	public function __construct(IConfig $config, IUserManager $userManager,
63
			FaceMapper $faceMapper, ImageMapper $imageMapper, PersonMapper $personMapper) {
64 1
		$this->config = $config;
65 1
		$this->userManager = $userManager;
66 1
		$this->faceMapper = $faceMapper;
67 1
		$this->imageMapper = $imageMapper;
68 1
		$this->personMapper = $personMapper;
69 1
	}
70
71
	/**
72
	 * Deletes all faces, images and persons found. IF no user is given, resetting is executed for all users.
73
	 *
74
	 * @param IUser|null $user Optional user to execute resetting for
75
	 */
76
	public function resetAll(IUser $user = null) {
77
		$eligable_users = array();
78
		if (is_null($user)) {
79
			$this->userManager->callForAllUsers(function (IUser $user) use (&$eligable_users) {
80
				$eligable_users[] = $user->getUID();
81
			});
82
		} else {
83
			$eligable_users[] = $user->getUID();
84
		}
85
86
		foreach($eligable_users as $user) {
87
			$this->resetAllForUser($user);
88
		}
89
	}
90
91
	/**
92
	 * Deletes all faces, images and persons found for a given user.
93
	 *
94
	 * @param string $user ID of user to execute resetting for
95
	 */
96 1
	public function resetAllForUser(string $userId) {
97 1
		$this->faceMapper->deleteUserFaces($userId);
98 1
		$this->personMapper->deleteUserPersons($userId);
99 1
		$this->imageMapper->deleteUserImages($userId);
100 1
		$this->config->deleteUserValue($userId, 'facerecognition', AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY);
101
	}
102
}