Passed
Push — master ( a86ea0...028966 )
by Matias
05:02
created

DisabledUserRemovalTask::execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 1
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019-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
namespace OCA\FaceRecognition\BackgroundJob\Tasks;
24
25
use OCP\IUser;
26
27
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask;
28
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext;
29
30
use OCA\FaceRecognition\Db\ImageMapper;
31
32
use OCA\FaceRecognition\Service\FaceManagementService;
33
use OCA\FaceRecognition\Service\SettingsService;
34
35
/**
36
 * Task that, for each user, check if disabled the analysis,
37
 * and if necessary remove data from this application
38
 */
39
class DisabledUserRemovalTask extends FaceRecognitionBackgroundTask {
40
41
	/** @var ImageMapper Image mapper */
42
	private $imageMapper;
43
44
	/** @var FaceManagementService */
45
	private $faceManagementService;
46
47
	/** @var SettingsService */
48
	private $settingsService;
49
50
	/**
51
	 * @param ImageMapper $imageMapper Image mapper
52
	 * @param FaceManagementService $faceManagementService
53
	 * @param SettingsService $settingsService
54
	 */
55 1
	public function __construct (ImageMapper           $imageMapper,
56
	                             FaceManagementService $faceManagementService,
57
	                             SettingsService       $settingsService)
58
	{
59 1
		parent::__construct();
60
61 1
		$this->imageMapper           = $imageMapper;
62 1
		$this->faceManagementService = $faceManagementService;
63 1
		$this->settingsService       = $settingsService;
64
	}
65
66
	/**
67
	 * @inheritdoc
68
	 */
69 1
	public function description() {
70 1
		return "Purge all the information of a user when disable the analysis.";
71
	}
72
73
	/**
74
	 * @inheritdoc
75
	 */
76 1
	public function execute(FaceRecognitionContext $context) {
77 1
		$this->setContext($context);
78
79
		// Check if we are called for one user only, or for all user in instance.
80 1
		$eligable_users = $this->context->getEligibleUsers();
81
82
		// Reset user datas if needed.
83 1
		foreach($eligable_users as $userId) {
84 1
			$userEnabled = $this->settingsService->getUserEnabled($userId);
85 1
			$imageCount = $this->imageMapper->countUserImages($userId, $this->settingsService->getCurrentFaceModel());
86 1
			if (!$userEnabled && $imageCount > 0) {
87
				// TODO: Check that the user really has information to remove.
88 1
				$this->logInfo(sprintf('Removing data from user %s that disable analysis', $userId));
89 1
				$this->faceManagementService->resetAllForUser($userId);
90
			}
91 1
			yield;
92
		}
93
94 1
		return true;
95
	}
96
97
}
98