Completed
Push — exception_in_tasks ( 7463cd...3899e4 )
by Branko
01:58
created

FaceNewMapper::getPersonOnFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 6
cp 0
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
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
	public function __construct(IDBConnection $db) {
11
		parent::__construct($db, 'face_recognition_faces', '\OCA\FaceRecognition\Db\FaceNew');
12
	}
13
14
	public function countFaces(string $userId, $model): int {
15
		$qb = $this->db->getQueryBuilder();
16
		$query = $qb
17
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('f.id') . ')'))
18
			->from('face_recognition_faces', 'f')
19
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
20
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
21
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
22
			->setParameter('user', $userId)
23
			->setParameter('model', $model);
24
		$resultStatement = $query->execute();
25
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
26
		$resultStatement->closeCursor();
27
28
		return (int)$data[0];
29
	}
30
31
	public function getFaces(string $userId, $model): array {
32
		$qb = $this->db->getQueryBuilder();
33
		$query = $qb
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
			->select('f.id', 'f.person', 'f.descriptor')
35
			->from('face_recognition_faces', 'f')
36
			->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
37
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
38
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
39
			->setParameter('user', $userId)
40
			->setParameter('model', $model);
41
		$faces = $this->frFindEntities($qb);
42
		return $faces;
43
	}
44
45
	/**
46
	 * @param int $imageId Image for which to delete faces for
47
	 */
48
	public function removeFaces(int $imageId) {
49
		$qb = $this->db->getQueryBuilder();
50
		$qb->delete($this->getTableName())
51
			->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId)))
52
			->execute();
53
	}
54
55
	/**
56
	 * Runs a sql query and returns an array of entities
57
	 *
58
	 * todo: stolen from QBMapper. However, this class is in use from 14.0 only.
59
	 * If we use it, we are "locked" ourselves to versions >= 14.0
60
	 *
61
	 * @param IQueryBuilder $query
62
	 * @return Entity[] all fetched entities
63
	 * @since 14.0.0
64
	 */
65
	protected function frFindEntities(IQueryBuilder $query): array {
66
		$cursor = $query->execute();
67
68
		$entities = [];
69
70
		while($row = $cursor->fetch()){
71
			$entities[] = $this->mapRowToEntity($row);
72
		}
73
74
		$cursor->closeCursor();
75
76
		return $entities;
77
	}
78
}
79