Passed
Push — recreate-cluster-logic ( 25ad8b )
by Branko
01:54
created

FaceNewMapper::getOldestCreatedFaceWithoutPerson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 2
dl 0
loc 12
ccs 0
cts 11
cp 0
crap 2
rs 9.9
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 FaceNewMapper extends Mapper {
10 4
	public function __construct(IDBConnection $db) {
11 4
		parent::__construct($db, 'face_recognition_faces', '\OCA\FaceRecognition\Db\FaceNew');
12 4
	}
13
14
	public function find (int $faceId): FaceNew {
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\FaceNew.
Loading history...
23
	}
24
25
	/**
26
	 * Counts all the faces that belong to images of a given user, created using given model
27
	 *
28
	 * @param string $userId User to which faces and associated images belongs to
29
	 * @param int $model Model ID
30
	 * @param bool $onlyWithoutPersons True if we need to count only faces which are not having person associated for it.
31
	 * If false, all faces are counted.
32
	 */
33
	public function countFaces(string $userId, int $model, bool $onlyWithoutPersons=false): int {
34
		$qb = $this->db->getQueryBuilder();
35
		$qb = $qb
36
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('f.id') . ')'))
37
			->from('face_recognition_faces', 'f')
38
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
39
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
40
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')));
41
		if ($onlyWithoutPersons) {
42
			$qb = $qb->andWhere($qb->expr()->isNull('person'));
43
		}
44
		$query = $qb
45
			->setParameter('user', $userId)
46
			->setParameter('model', $model);
47
		$resultStatement = $query->execute();
48
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
49
		$resultStatement->closeCursor();
50
51
		return (int)$data[0];
52
	}
53
54
	/**
55
	 * Gets oldest created face from database, for a given user and model, that is not associated with a person.
56
	 *
57
	 * @param string $userId User to which faces and associated images belongs to
58
	 * @param int $model Model ID
59
	 *
60
	 * @return FaceNew Oldest face, if any is found
61
	 * @throws DoesNotExistException If there is no faces in database without person for a given user and model.
62
	 */
63
	public function getOldestCreatedFaceWithoutPerson(string $userId, int $model): FaceNew {
64
		$qb = $this->db->getQueryBuilder();
65
		$qb
66
			->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

66
			->/** @scrutinizer ignore-call */ 
67
     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...
67
			->from('face_recognition_faces', 'f')
68
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
69
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
70
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
71
			->andWhere($qb->expr()->isNull('person'));
72
		$params = array('user' => $userId, 'model' => $model);
73
		$face = $this->findEntity($qb->getSQL(), $params, 1);
74
		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\FaceNew.
Loading history...
75
	}
76
77
	public function getFaces(string $userId, $model): array {
78
		$qb = $this->db->getQueryBuilder();
79
		$query = $qb
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
80
			->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

80
			->/** @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...
81
			->from('face_recognition_faces', 'f')
82
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
83
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
84
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
85
			->setParameter('user', $userId)
86
			->setParameter('model', $model);
87
		$faces = $this->frFindEntities($qb);
88
		return $faces;
89
	}
90
91
	public function getPersonOnFile(string $userId, int $personId, int $fileId, int $model): array {
92
		$qb = $this->db->getQueryBuilder();
93
		$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

93
		$qb->/** @scrutinizer ignore-call */ 
94
       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...
94
			->from('face_recognition_faces', 'f')
95
			->innerJoin('f', 'face_recognition_persons' ,'p', $qb->expr()->eq('f.person', 'p.id'))
96
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
97
			->where($qb->expr()->eq('p.user', $qb->createParameter('user')))
98
			->andWhere($qb->expr()->eq('person', $qb->createParameter('person')))
99
			->andWhere($qb->expr()->eq('file', $qb->createParameter('file_id')))
100
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
101
			->andWhere($qb->expr()->eq('p.is_valid', $qb->createParameter('is_valid')))
102
			->setParameter('user', $userId)
103
			->setParameter('person', $personId)
104
			->setParameter('file_id', $fileId)
105
			->setParameter('model', $model)
106
			->setParameter('is_valid', true);
107
		$faces = $this->frFindEntities($qb);
108
		return $faces;
109
	}
110
111
	/**
112
	 * @param int $imageId Image for which to delete faces for
113
	 */
114
	public function removeFaces(int $imageId) {
115
		$qb = $this->db->getQueryBuilder();
116
		$qb->delete($this->getTableName())
117
			->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId)))
118
			->execute();
119
	}
120
121
	/**
122
	 * Runs a sql query and returns an array of entities
123
	 *
124
	 * todo: stolen from QBMapper. However, this class is in use from 14.0 only.
125
	 * If we use it, we are "locked" ourselves to versions >= 14.0
126
	 *
127
	 * @param IQueryBuilder $query
128
	 * @return Entity[] all fetched entities
129
	 * @since 14.0.0
130
	 */
131
	protected function frFindEntities(IQueryBuilder $query): array {
132
		$cursor = $query->execute();
133
134
		$entities = [];
135
136
		while($row = $cursor->fetch()){
137
			$entities[] = $this->mapRowToEntity($row);
138
		}
139
140
		$cursor->closeCursor();
141
142
		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...
143
	}
144
145
}
146