Passed
Pull Request — master (#549)
by Matias
07:06 queued 04:58
created

DavService::getFilesNamedFilter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 12
rs 9.9666
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2021 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
24
namespace OCA\FaceRecognition\Service;
25
26
use OC\Files\View;
0 ignored issues
show
Bug introduced by
The type OC\Files\View 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...
27
use OCA\DAV\Connector\Sabre\File;
0 ignored issues
show
Bug introduced by
The type OCA\DAV\Connector\Sabre\File 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...
28
use OCA\DAV\Connector\Sabre\Node as SabreNode;
0 ignored issues
show
Bug introduced by
The type OCA\DAV\Connector\Sabre\Node 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...
29
30
use Sabre\DAV\INode;
0 ignored issues
show
Bug introduced by
The type Sabre\DAV\INode 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...
31
use Sabre\DAV\PropFind;
0 ignored issues
show
Bug introduced by
The type Sabre\DAV\PropFind 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...
32
33
use OCA\FaceRecognition\AppInfo\Application;
34
35
use OCA\FaceRecognition\Db\Image;
36
use OCA\FaceRecognition\Db\ImageMapper;
37
38
use OCA\FaceRecognition\Db\Face;
39
use OCA\FaceRecognition\Db\FaceMapper;
40
41
use OCA\FaceRecognition\Db\Person;
42
use OCA\FaceRecognition\Db\PersonMapper;
43
44
use OCA\FaceRecognition\Service\FileService;
45
46
use OCA\FaceRecognition\Service\SettingsService;
47
48
use OCA\FaceRecognition\Dav\PersonsList;
49
50
class DavService {
51
52
	/** @var ImageMapper */
53
	private $imageMapper;
54
55
	/** @var PersonMapper */
56
	private $personMapper;
57
58
	/** @var FaceMapper */
59
	private $faceMapper;
60
61
	/** @var FileService */
62
	private $fileService;
63
64
	/** @var SettingsService */
65
	private $settingsService;
66
67
	private $userId;
68
69
	/**
70
	 * @var string
71
	 */
72
	private $appName;
73
74
	/**
75
	 * DavService constructor.
76
	 * @param string $appName
77
	 * @param string|null $userId
78
	 */
79
	public function __construct(string          $appName,
80
	                            ImageMapper     $imageMapper,
81
	                            PersonMapper    $personMapper,
82
	                            FaceMapper      $faceMapper,
83
	                            FileService     $fileService,
84
	                            SettingsService $settingsService,
85
	                            ?string         $userId)
86
	{
87
		$this->appName         = $appName;
88
		$this->imageMapper     = $imageMapper;
89
		$this->personMapper    = $personMapper;
90
		$this->faceMapper      = $faceMapper;
91
		$this->fileService     = $fileService;
92
		$this->settingsService = $settingsService;
93
		$this->userId          = $userId;
94
	}
95
96
	/**
97
	 * Get Face Recognition state of a given file for a given user
98
	 * @param int $fileId
99
	 * @param string|null $userId
100
	 * @return array state and rule id
101
	 */
102
	public function getFaceRecognitionImage(int $fileId, ?string $userId): array {
103
		$response = array();
104
		if (is_null($userId)) {
105
			$response['state'] = Application::STATE_UNDEFINED;
106
			return $response;
107
		}
108
109
		if (!$this->settingsService->getUserEnabled($userId)) {
110
			$response['state'] = Application::STATE_DISABLED;
111
			return $response;
112
		}
113
114
		$modelId = $this->settingsService->getCurrentFaceModel();
115
		$image = $this->imageMapper->findFromFile($userId, $modelId, $fileId);
116
		if (!$image || !$image->getIsProcessed()) {
0 ignored issues
show
introduced by
$image is of type OCA\FaceRecognition\Db\Image, thus it always evaluated to true.
Loading history...
117
			$response['state'] = Application::STATE_UNDEFINED;
118
			return $response;
119
		}
120
121
		$personsImage = array();
122
		$faces = $this->faceMapper->findFromFile($userId, $modelId, $fileId);
123
		foreach ($faces as $face) {
124
			$person = $this->personMapper->find($this->userId, $face->getPerson());
0 ignored issues
show
Bug introduced by
It seems like $this->userId can also be of type null; however, parameter $userId of OCA\FaceRecognition\Db\PersonMapper::find() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
			$person = $this->personMapper->find(/** @scrutinizer ignore-type */ $this->userId, $face->getPerson());
Loading history...
125
			if (!$person->getIsVisible()) {
126
				continue;
127
			}
128
129
			$facePerson = array();
130
			$facePerson['id'] = $person->getId();
131
			$facePerson['name'] = $person->getName();
132
			$facePerson['top'] = $face->getTop();
133
			$facePerson['left'] = $face->getLeft();
134
			$facePerson['width'] = $face->getRight() - $face->getLeft();
135
			$facePerson['height'] = $face->getBottom() - $face->getTop();
136
137
			$personsImage[] = $facePerson;
138
		}
139
140
		if (count($personsImage)) {
141
			$response['persons'] = $personsImage;
142
			return $response;
143
		}
144
145
		$response['state'] = Application::STATE_NO_PERSONS;
146
		return $response;
147
	}
148
149
	/**
150
	 * Get persons as a WebDav attribute
151
	 *
152
	 * @param PropFind $propFind
153
	 * @param INode $node
154
	 * @return void
155
	 */
156
	public function propFind(PropFind $propFind, INode $node): void {
157
		if (!$node instanceof SabreNode) {
158
			return;
159
		}
160
161
		$nodeId = $node->getId();
162
		$image = $this->getFaceRecognitionImage($nodeId, $this->userId);
163
164
		$propFind->handle(
165
			Application::DAV_PROPERTY_FACES, function() use ($image) {
166
				if (isset($image['persons'])) {
167
					return new PersonsList($image['persons']);
168
				}
169
				return $image['state'];
170
			}
171
		);
172
	}
173
174
	/**
175
	 * Get files filtered by name
176
	 *
177
	 */
178
	public function getFilesNamedFilter(array $filterNames): array {
179
		$modelId = $this->settingsService->getCurrentFaceModel();
180
		$view = \OC\Files\Filesystem::getView();
0 ignored issues
show
Bug introduced by
The type OC\Files\Filesystem 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...
181
		$files = [];
182
		foreach ($filterNames as $name) {
183
			$images = $this->imageMapper->findFromPerson($this->userId, $modelId, $name);
0 ignored issues
show
Bug introduced by
It seems like $this->userId can also be of type null; however, parameter $userId of OCA\FaceRecognition\Db\I...apper::findFromPerson() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

183
			$images = $this->imageMapper->findFromPerson(/** @scrutinizer ignore-type */ $this->userId, $modelId, $name);
Loading history...
184
			foreach ($images as $image) {
185
				$file = $this->fileService->getFileById($image->getFile(), $this->userId);
186
				$files[] = new File($view, $file);
187
			}
188
		}
189
190
		return $files;
191
	}
192
193
}
194