Passed
Push — davplugin ( c34882 )
by Matias
12:05
created

DavService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 2
rs 10
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 OCA\FaceRecognition\AppInfo\Application;
27
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
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...
30
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...
31
32
use OCA\FaceRecognition\Db\Image;
33
use OCA\FaceRecognition\Db\ImageMapper;
34
35
use OCA\FaceRecognition\Db\Face;
36
use OCA\FaceRecognition\Db\FaceMapper;
37
38
use OCA\FaceRecognition\Db\Person;
39
use OCA\FaceRecognition\Db\PersonMapper;
40
41
use OCA\FaceRecognition\Service\FileService;
42
43
use OCA\FaceRecognition\Service\SettingsService;
44
45
use OCA\FaceRecognition\Dav\PersonsList;
46
47
class DavService {
48
49
	/** @var ImageMapper */
50
	private $imageMapper;
51
52
	/** @var PersonMapper */
53
	private $personMapper;
54
55
	/** @var FaceMapper */
56
	private $faceMapper;
57
58
	/** @var FileService */
59
	private $fileService;
60
61
	/** @var SettingsService */
62
	private $settingsService;
63
64
	private $userId;
65
66
	/**
67
	 * @var string
68
	 */
69
	private $appName;
70
71
	/**
72
	 * DavService constructor.
73
	 * @param string $appName
74
	 * @param string|null $userId
75
	 */
76
	public function __construct(string          $appName,
77
	                            ImageMapper     $imageMapper,
78
	                            PersonMapper    $personMapper,
79
	                            FaceMapper      $faceMapper,
80
	                            FileService     $fileService,
81
	                            SettingsService $settingsService,
82
	                            ?string         $userId)
83
	{
84
		$this->appName         = $appName;
85
		$this->imageMapper     = $imageMapper;
86
		$this->personMapper    = $personMapper;
87
		$this->faceMapper      = $faceMapper;
88
		$this->fileService     = $fileService;
89
		$this->settingsService = $settingsService;
90
		$this->userId          = $userId;
91
	}
92
93
	/**
94
	 * Get approval state of a given file for a given user
95
	 * @param int $fileId
96
	 * @param string|null $userId
97
	 * @return array state and rule id
98
	 */
99
	public function getFaceRecognitionImage(int $fileId, ?string $userId): array {
100
		$response = array();
101
		if (is_null($userId)) {
102
			$response['state'] = Application::STATE_UNDEFINED;
103
			return $response;
104
		}
105
106
		if (!$this->settingsService->getUserEnabled($userId)) {
107
			$response['state'] = Application::STATE_DISABLED;
108
			return $response;
109
		}
110
111
		$modelId = $this->settingsService->getCurrentFaceModel();
112
		$image = $this->imageMapper->findFromFile($userId, $modelId, $fileId);
113
		if (!$image->getIsProcessed()) {
114
			$response['state'] = Application::STATE_UNDEFINED;
115
			return $response;
116
		}
117
118
		$personsImage = array();
119
		$faces = $this->faceMapper->findFromFile($userId, $modelId, $fileId);
120
		foreach ($faces as $face) {
121
			$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

121
			$person = $this->personMapper->find(/** @scrutinizer ignore-type */ $this->userId, $face->getPerson());
Loading history...
122
			if (!$person->getIsVisible()) {
123
				continue;
124
			}
125
126
			$facePerson = array();
127
			$facePerson['id'] = $person->getId();
128
			$facePerson['name'] = $person->getName();
129
			$facePerson['top'] = $face->getTop();
130
			$facePerson['left'] = $face->getLeft();
131
			$facePerson['width'] = $face->getRight() - $face->getLeft();
132
			$facePerson['height'] = $face->getBottom() - $face->getTop();
133
134
			$personsImage[] = $facePerson;
135
		}
136
137
		if (count($personsImage)) {
138
			$response['persons'] = $personsImage;
139
			return $response;
140
		}
141
142
		$response['state'] = Application::STATE_NO_PERSONS;
143
		return $response;
144
	}
145
146
	/**
147
	 * Get persons as a WebDav attribute
148
	 *
149
	 * @param PropFind $propFind
150
	 * @param INode $node
151
	 * @return void
152
	 */
153
	public function propFind(PropFind $propFind, INode $node): void {
154
		if (!$node instanceof SabreNode) {
155
			return;
156
		}
157
158
		$nodeId = $node->getId();
159
		$image = $this->getFaceRecognitionImage($nodeId, $this->userId);
160
		$propFind->handle(
161
			Application::DAV_PROPERTY_PERSONS, function() use ($nodeId, $image) {
0 ignored issues
show
Unused Code introduced by
The import $nodeId is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
162
				if (isset($image['persons'])) {
163
					return new PersonsList($image['persons']);
164
				}
165
				return $image['state'];
166
			}
167
		);
168
	}
169
170
}
171