Passed
Pull Request — master (#178)
by Branko
06:23 queued 04:49
created

Watcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 1
eloc 8
c 4
b 2
f 0
nc 1
nop 8
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]>
4
 * @copyright Copyright (c) 2017, Matias De lellis <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 * @author Matias De lellis <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
namespace OCA\FaceRecognition;
26
27
use OCP\Files\Folder;
28
use OCP\Files\IHomeStorage;
29
use OCP\Files\Node;
30
use OCP\IConfig;
31
use OCP\IDBConnection;
32
use OCP\ILogger;
33
use OCP\IUserManager;
34
35
use OCA\FaceRecognition\FaceManagementService;
36
37
use OCA\FaceRecognition\BackgroundJob\Tasks\AddMissingImagesTask;
38
use OCA\FaceRecognition\BackgroundJob\Tasks\StaleImagesRemovalTask;
39
use OCA\FaceRecognition\Db\Face;
40
use OCA\FaceRecognition\Db\Image;
41
use OCA\FaceRecognition\Db\FaceMapper;
42
use OCA\FaceRecognition\Db\ImageMapper;
43
use OCA\FaceRecognition\Db\PersonMapper;
44
use OCA\FaceRecognition\Helper\Requirements;
45
use OCA\FaceRecognition\Migration\AddDefaultFaceModel;
46
47
class Watcher {
48
49
	/** @var IConfig Config */
50
	private $config;
51
52
	/** @var ILogger Logger */
53
	private $logger;
54
55
	/** @var IDBConnection */
56
	private $connection;
57
58
	/** @var IUserManager */
59
	private $userManager;
60
61
	/** @var FaceMapper */
62
	private $faceMapper;
63
64
	/** @var ImageMapper */
65
	private $imageMapper;
66
67
	/** @var PersonMapper */
68
	private $personMapper;
69
70
	/** @var FaceManagementService */
71
	private $faceManagementService;
72
73
	/**
74
	 * Watcher constructor.
75
	 *
76
	 * @param IConfig $config
77
	 * @param ILogger $logger
78
	 * @param IDBConnection $connection
79
	 * @param IUserManager $userManager
80
	 * @param FaceMapper $faceMapper
81
	 * @param ImageMapper $imageMapper
82
	 * @param PersonMapper $personMapper
83
	 * @param FaceManagementService $faceManagementService
84
	 */
85 28
	public function __construct(IConfig               $config,
86
	                            ILogger               $logger,
87
	                            IDBConnection         $connection,
88
	                            IUserManager          $userManager,
89
	                            FaceMapper            $faceMapper,
90
	                            ImageMapper           $imageMapper,
91
	                            PersonMapper          $personMapper,
92
	                            FaceManagementService $faceManagementService)
93
	{
94 28
		$this->config = $config;
95 28
		$this->logger = $logger;
96 28
		$this->connection = $connection;
97 28
		$this->userManager = $userManager;
98 28
		$this->faceMapper = $faceMapper;
99 28
		$this->imageMapper = $imageMapper;
100 28
		$this->personMapper = $personMapper;
101 28
		$this->faceManagementService = $faceManagementService;
102 28
	}
103
104
	/**
105
	 * A node has been updated. We just store the file id
106
	 * with the current user in the DB
107
	 *
108
	 * @param Node $node
109
	 */
110 28
	public function postWrite(Node $node) {
111 28
		$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
112
113
		// todo: should we also care about this too: instanceOfStorage(ISharedStorage::class);
114 28
		if ($node->getStorage()->instanceOfStorage(IHomeStorage::class) === false) {
115 28
			return;
116
		}
117
118 28
		if ($node instanceof Folder) {
119 28
			return;
120
		}
121
122
		$owner = $node->getOwner()->getUid();
123
124
		$enabled = $this->config->getUserValue($owner, 'facerecognition', 'enabled', 'false');
125
		if ($enabled !== 'true') {
126
			$this->logger->debug('The user ' . $owner . ' not have the analysis enabled. Skipping');
127
			return;
128
		}
129
130
		if ($node->getName() === '.nomedia') {
131
			// If user added this file, it means all images in this and all child directories should be removed.
132
			// Instead of doing that here, it's better to just add flag that image removal should be done.
133
			$this->config->setUserValue($owner, 'facerecognition', StaleImagesRemovalTask::STALE_IMAGES_REMOVAL_NEEDED_KEY, 'true');
134
			return;
135
		}
136
137
		if (!Requirements::isImageTypeSupported($node->getMimeType())) {
138
			return;
139
		}
140
141
		if (!$this->userManager->userExists($owner)) {
142
			$this->logger->debug(
143
				"Skipping inserting image " . $node->getName() . " because it seems that user  " . $owner . " doesn't exist");
144
			return;
145
		}
146
147
		// If we detect .nomedia file anywhere on the path to root folder (id===null), bail out
148
		$parentNode = $node->getParent();
149
		while (($parentNode instanceof Folder) && ($parentNode->getId() !== null)) {
150
			if ($parentNode->nodeExists('.nomedia')) {
151
				$this->logger->debug(
152
					"Skipping inserting image " . $node->getName() . " because directory " . $parentNode->getName() . " contains .nomedia file");
153
				return;
154
			}
155
156
			$parentNode = $parentNode->getParent();
157
		}
158
159
		$this->logger->debug("Inserting/updating image " . $node->getName() . " for face recognition");
160
161
		$image = new Image();
162
		$image->setUser($owner);
163
		$image->setFile($node->getId());
164
		$image->setModel($model);
165
166
		$imageId = $this->imageMapper->imageExists($image);
167
		if ($imageId === null) {
168
			// todo: can we have larger transaction with bulk insert?
169
			$this->imageMapper->insert($image);
170
		} else {
171
			$this->imageMapper->resetImage($image);
172
			// note that invalidatePersons depends on existence of faces for a given image,
173
			// and we must invalidate before we delete faces!
174
			$this->personMapper->invalidatePersons($imageId);
175
176
			// Fetch all faces to be deleted before deleting them, and then delete them
177
			$facesToRemove = $this->faceMapper->findByImage($imageId);
178
			$this->faceMapper->removeFaces($imageId);
179
180
			// If any person is now without faces, remove those (empty) persons
181
			foreach ($facesToRemove as $faceToRemove) {
182
				if ($faceToRemove->getPerson() !== null) {
183
					$this->personMapper->removeIfEmpty($faceToRemove->getPerson());
184
				}
185
			}
186
		}
187
	}
188
189
	/**
190
	 * A node has been deleted. Remove faces with file id
191
	 * with the current user in the DB
192
	 *
193
	 * @param Node $node
194
	 */
195
	public function postDelete(Node $node) {
196
		$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
197
198
		// todo: should we also care about this too: instanceOfStorage(ISharedStorage::class);
199
		if ($node->getStorage()->instanceOfStorage(IHomeStorage::class) === false) {
200
			return;
201
		}
202
203
		if ($node instanceof Folder) {
204
			return;
205
		}
206
207
		$owner = $node->getOwner()->getUid();
208
209
		$enabled = $this->config->getUserValue($owner, 'facerecognition', 'enabled', 'false');
210
		if ($enabled !== 'true') {
211
			$this->logger->debug('The user ' . $owner . ' not have the analysis enabled. Skipping');
212
			return;
213
		}
214
215
		if ($node->getName() === '.nomedia') {
216
			// If user deleted file named .nomedia, that means all images in this and all child directories should be added.
217
			// But, instead of doing that here, better option seem to be to just reset flag that image scan is not done.
218
			// This will trigger another round of image crawling in AddMissingImagesTask for this user and those images will be added.
219
			$this->config->setUserValue($owner, 'facerecognition', AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY, 'false');
220
			return;
221
		}
222
223
		if (!Requirements::isImageTypeSupported($node->getMimeType())) {
224
			return;
225
		}
226
227
		$this->logger->debug("Deleting image " . $node->getName() . " from face recognition");
228
229
		$image = new Image();
230
		$image->setUser($owner);
231
		$image->setFile($node->getId());
232
		$image->setModel($model);
233
234
		$imageId = $this->imageMapper->imageExists($image);
235
		if ($imageId !== null) {
236
			// note that invalidatePersons depends on existence of faces for a given image,
237
			// and we must invalidate before we delete faces!
238
			$this->personMapper->invalidatePersons($imageId);
239
240
			// Fetch all faces to be deleted before deleting them, and then delete them
241
			$facesToRemove = $this->faceMapper->findByImage($imageId);
242
			$this->faceMapper->removeFaces($imageId);
243
244
			$image->setId($imageId);
245
			$this->imageMapper->delete($image);
246
247
			// If any person is now without faces, remove those (empty) persons
248
			foreach ($facesToRemove as $faceToRemove) {
249
				if ($faceToRemove->getPerson() !== null) {
250
					$this->personMapper->removeIfEmpty($faceToRemove->getPerson());
251
				}
252
			}
253
		}
254
	}
255
256
	/**
257
	 * A user has been deleted. Cleanup everything from this user.
258
	 *
259
	 * @param \OC\User\User $user Deleted user
260
	 */
261 28
	public function postUserDelete(\OC\User\User $user) {
0 ignored issues
show
Bug introduced by
The type OC\User\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
262 28
		$userId = $user->getUid();
263 28
		$this->faceManagementService->resetAllForUser($userId);
264 28
		$this->logger->info("Removed all face recognition data for deleted user " . $userId);
265 28
	}
266
}
267