Completed
Push — master ( faa25e...f0e399 )
by Branko
18s queued 11s
created

FaceNewMapper::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 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
	public function countFaces(string $userId, $model): int {
26
		$qb = $this->db->getQueryBuilder();
27
		$query = $qb
28
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('f.id') . ')'))
29
			->from('face_recognition_faces', 'f')
30
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
31
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
32
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
33
			->setParameter('user', $userId)
34
			->setParameter('model', $model);
35
		$resultStatement = $query->execute();
36
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
37
		$resultStatement->closeCursor();
38
39
		return (int)$data[0];
40
	}
41
42
	public function getFaces(string $userId, $model): array {
43
		$qb = $this->db->getQueryBuilder();
44
		$query = $qb
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
45
			->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

45
			->/** @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...
46
			->from('face_recognition_faces', 'f')
47
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
48
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
49
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
50
			->setParameter('user', $userId)
51
			->setParameter('model', $model);
52
		$faces = $this->frFindEntities($qb);
53
		return $faces;
54
	}
55
56
	public function getPersonOnFile(string $userId, int $personId, int $fileId, int $model): array {
57
		$qb = $this->db->getQueryBuilder();
58
		$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

58
		$qb->/** @scrutinizer ignore-call */ 
59
       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...
59
			->from('face_recognition_faces', 'f')
60
			->innerJoin('f', 'face_recognition_persons' ,'p', $qb->expr()->eq('f.person', 'p.id'))
61
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
62
			->where($qb->expr()->eq('p.user', $qb->createParameter('user')))
63
			->andWhere($qb->expr()->eq('person', $qb->createParameter('person')))
64
			->andWhere($qb->expr()->eq('file', $qb->createParameter('file_id')))
65
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
66
			->andWhere($qb->expr()->eq('p.is_valid', $qb->createParameter('is_valid')))
67
			->setParameter('user', $userId)
68
			->setParameter('person', $personId)
69
			->setParameter('file_id', $fileId)
70
			->setParameter('model', $model)
71
			->setParameter('is_valid', true);
72
		$faces = $this->frFindEntities($qb);
73
		return $faces;
74
	}
75
76
	/**
77
	 * @param int $imageId Image for which to delete faces for
78
	 */
79
	public function removeFaces(int $imageId) {
80
		$qb = $this->db->getQueryBuilder();
81
		$qb->delete($this->getTableName())
82
			->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId)))
83
			->execute();
84
	}
85
86
	/**
87
	 * Runs a sql query and returns an array of entities
88
	 *
89
	 * todo: stolen from QBMapper. However, this class is in use from 14.0 only.
90
	 * If we use it, we are "locked" ourselves to versions >= 14.0
91
	 *
92
	 * @param IQueryBuilder $query
93
	 * @return Entity[] all fetched entities
94
	 * @since 14.0.0
95
	 */
96
	protected function frFindEntities(IQueryBuilder $query): array {
97
		$cursor = $query->execute();
98
99
		$entities = [];
100
101
		while($row = $cursor->fetch()){
102
			$entities[] = $this->mapRowToEntity($row);
103
		}
104
105
		$cursor->closeCursor();
106
107
		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...
108
	}
109
110
}
111