Passed
Push — drop-old-code ( bf8145...f99a4d )
by Matias
02:11
created

FaceMapper::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 9
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace OCA\FaceRecognition\Db;
3
4
use OCP\IDBConnection;
5
use OCP\AppFramework\Db\Mapper;
6
use OCP\AppFramework\Db\DoesNotExistException;
7
use OCP\DB\QueryBuilder\IQueryBuilder;
8
9
class FaceMapper extends Mapper {
10 4
	public function __construct(IDBConnection $db) {
11 4
		parent::__construct($db, 'face_recognition_faces', '\OCA\FaceRecognition\Db\Face');
12 4
	}
13
14
	public function find (int $faceId): Face {
15
		$qb = $this->db->getQueryBuilder();
16
		$qb->select('id', 'image', 'person', 'left', 'right', 'top', 'bottom', 'descriptor')
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::select() has too many arguments starting with 'image'. ( Ignorable by Annotation )

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

16
		$qb->/** @scrutinizer ignore-call */ 
17
       select('id', 'image', 'person', 'left', 'right', 'top', 'bottom', 'descriptor')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
17
			->from('face_recognition_faces', 'f')
18
			->andWhere($qb->expr()->eq('id', $qb->createParameter('face_id')));
19
		$params = array();
20
		$params['face_id'] = $faceId;
21
		$faces = $this->findEntity($qb->getSQL(), $params);
22
		return $faces;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $faces returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\FaceRecognition\Db\Face.
Loading history...
23
	}
24
25
	public function findAllFromPerson (int $personId): array {
26
		$qb = $this->db->getQueryBuilder();
27
		$qb->select('id', 'image', 'person', 'left', 'right', 'top', 'bottom', 'descriptor')
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::select() has too many arguments starting with 'image'. ( Ignorable by Annotation )

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

27
		$qb->/** @scrutinizer ignore-call */ 
28
       select('id', 'image', 'person', 'left', 'right', 'top', 'bottom', 'descriptor')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
28
			->from('face_recognition_faces', 'f')
29
			->where($qb->expr()->eq('person', $qb->createParameter('person_id')));
30
31
		$params = array();
32
		$params['person_id'] = $personId;
33
34
		$faces = $this->findEntities($qb->getSQL(), $params);
35
		return $faces;
36
	}
37
38
	/**
39
	 * Counts all the faces that belong to images of a given user, created using given model
40
	 *
41
	 * @param string $userId User to which faces and associated images belongs to
42
	 * @param int $model Model ID
43
	 * @param bool $onlyWithoutPersons True if we need to count only faces which are not having person associated for it.
44
	 * If false, all faces are counted.
45
	 */
46
	public function countFaces(string $userId, int $model, bool $onlyWithoutPersons=false): int {
47
		$qb = $this->db->getQueryBuilder();
48
		$qb = $qb
49
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('f.id') . ')'))
50
			->from('face_recognition_faces', 'f')
51
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
52
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
53
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')));
54
		if ($onlyWithoutPersons) {
55
			$qb = $qb->andWhere($qb->expr()->isNull('person'));
56
		}
57
		$query = $qb
58
			->setParameter('user', $userId)
59
			->setParameter('model', $model);
60
		$resultStatement = $query->execute();
61
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
62
		$resultStatement->closeCursor();
63
64
		return (int)$data[0];
65
	}
66
67
	/**
68
	 * Gets oldest created face from database, for a given user and model, that is not associated with a person.
69
	 *
70
	 * @param string $userId User to which faces and associated images belongs to
71
	 * @param int $model Model ID
72
	 *
73
	 * @return Face Oldest face, if any is found
74
	 * @throws DoesNotExistException If there is no faces in database without person for a given user and model.
75
	 */
76
	public function getOldestCreatedFaceWithoutPerson(string $userId, int $model): Face {
77
		$qb = $this->db->getQueryBuilder();
78
		$qb
79
			->select('f.id', 'f.creation_time')
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::select() has too many arguments starting with 'f.creation_time'. ( Ignorable by Annotation )

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

79
			->/** @scrutinizer ignore-call */ 
80
     select('f.id', 'f.creation_time')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
80
			->from('face_recognition_faces', 'f')
81
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
82
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
83
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
84
			->andWhere($qb->expr()->isNull('person'));
85
		$params = array('user' => $userId, 'model' => $model);
86
		$face = $this->findEntity($qb->getSQL(), $params, 1);
87
		return $face;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $face returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\FaceRecognition\Db\Face.
Loading history...
88
	}
89
90
	public function getFaces(string $userId, $model): array {
91
		$qb = $this->db->getQueryBuilder();
92
		$query = $qb
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
93
			->select('f.id', 'f.person', 'f.descriptor')
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::select() has too many arguments starting with 'f.person'. ( Ignorable by Annotation )

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

93
			->/** @scrutinizer ignore-call */ select('f.id', 'f.person', 'f.descriptor')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
94
			->from('face_recognition_faces', 'f')
95
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
96
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
97
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
98
			->setParameter('user', $userId)
99
			->setParameter('model', $model);
100
		$faces = $this->frFindEntities($qb);
101
		return $faces;
102
	}
103
104
	public function findFacesFromPerson(string $userId, int $personId, int $model, $limit = null, $offset = null): array {
105
		$qb = $this->db->getQueryBuilder();
106
		$qb->select('f.id', 'f.image', 'f.person')
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::select() has too many arguments starting with 'f.image'. ( Ignorable by Annotation )

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

106
		$qb->/** @scrutinizer ignore-call */ 
107
       select('f.id', 'f.image', 'f.person')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
107
			->from('face_recognition_faces', 'f')
108
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
109
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
110
			->andWhere($qb->expr()->eq('person', $qb->createParameter('person_id')))
111
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')));
112
113
		$params = array();
114
		$params['user'] = $userId;
115
		$params['person_id'] = $personId;
116
		$params['model'] = $model;
117
118
		$faces = $this->findEntities($qb->getSQL(), $params, $limit, $offset);
119
		return $faces;
120
	}
121
122
123
	public function getPersonOnFile(string $userId, int $personId, int $fileId, int $model): array {
124
		$qb = $this->db->getQueryBuilder();
125
		$qb->select('f.id', 'left', 'right', 'top', 'bottom')
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::select() has too many arguments starting with 'left'. ( Ignorable by Annotation )

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

125
		$qb->/** @scrutinizer ignore-call */ 
126
       select('f.id', 'left', 'right', 'top', 'bottom')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
126
			->from('face_recognition_faces', 'f')
127
			->innerJoin('f', 'face_recognition_persons' ,'p', $qb->expr()->eq('f.person', 'p.id'))
128
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
129
			->where($qb->expr()->eq('p.user', $qb->createParameter('user')))
130
			->andWhere($qb->expr()->eq('person', $qb->createParameter('person')))
131
			->andWhere($qb->expr()->eq('file', $qb->createParameter('file_id')))
132
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
133
			->andWhere($qb->expr()->eq('p.is_valid', $qb->createParameter('is_valid')))
134
			->setParameter('user', $userId)
135
			->setParameter('person', $personId)
136
			->setParameter('file_id', $fileId)
137
			->setParameter('model', $model)
138
			->setParameter('is_valid', true);
139
		$faces = $this->frFindEntities($qb);
140
		return $faces;
141
	}
142
143
	/**
144
	 * @param int $imageId Image for which to delete faces for
145
	 */
146
	public function removeFaces(int $imageId) {
147
		$qb = $this->db->getQueryBuilder();
148
		$qb->delete($this->getTableName())
149
			->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId)))
150
			->execute();
151
	}
152
153
	/**
154
	 * Runs a sql query and returns an array of entities
155
	 *
156
	 * todo: stolen from QBMapper. However, this class is in use from 14.0 only.
157
	 * If we use it, we are "locked" ourselves to versions >= 14.0
158
	 *
159
	 * @param IQueryBuilder $query
160
	 * @return Entity[] all fetched entities
161
	 * @since 14.0.0
162
	 */
163
	protected function frFindEntities(IQueryBuilder $query): array {
164
		$cursor = $query->execute();
165
166
		$entities = [];
167
168
		while($row = $cursor->fetch()){
169
			$entities[] = $this->mapRowToEntity($row);
170
		}
171
172
		$cursor->closeCursor();
173
174
		return $entities;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $entities returns an array which contains values of type OCP\AppFramework\Db\Entity which are incompatible with the documented value type OCA\FaceRecognition\Db\Entity.
Loading history...
175
	}
176
177
}
178