Passed
Pull Request — master (#55)
by Branko
02:18 queued 01:23
created

FaceMapper::findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
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
11 4
	public function __construct(IDBConnection $db) {
12 4
		parent::__construct($db, 'face_recognition', '\OCA\FaceRecognition\Db\Face');
13 4
	}
14
15
	public function find($id, $userId) {
16
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE id = ? AND uid = ?';
17
		return $this->findEntity($sql, [$id, $userId]);
18
	}
19
20
	public function findAll($userId) {
21
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE uid = ? and encoding IS NOT NULL';
22
		return $this->findEntities($sql, [$userId]);
23
	}
24
25
	public function findAllQueued($userId, $limit = null, $offset = null) {
26
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE uid = ? AND distance = -1 AND encoding IS NULL';
27
		return $this->findEntities($sql, [$userId], $limit, $offset);
28
	}
29
30
	public function findAllKnown($userId) {
31
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE uid = ? AND distance = 0 AND encoding IS NOT NULL';
32
		return $this->findEntities($sql, [$userId]);
33
	}
34
35
	public function findAllUnknown($userId) {
36
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE uid = ? AND distance > 0 AND encoding IS NOT NULL';
37
		return $this->findEntities($sql, [$userId]);
38
	}
39
40
	public function findAllEmpty($userId) {
41
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE uid = ? AND distance = 0 AND encoding IS NULL';
42
		return $this->findEntities($sql, [$userId]);
43
	}
44
45
	public function findAllNamed($userId, $query, $limit = null, $offset = null) {
46
		$sql = 'SELECT id, name, distance FROM *PREFIX*face_recognition WHERE uid = ? AND encoding IS NOT NULL AND LOWER(name) LIKE LOWER(?)';
47
		return $this->findEntities($sql, [$userId, $query], $limit, $offset);
48
	}
49
50
	public function findRandom($userId) {
51
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE uid = ? AND distance = 1 AND encoding IS NOT NULL ORDER BY RAND() LIMIT 8';
52
		return $this->findEntities($sql, [$userId]);
53
	}
54
55
	public function getGroups($userId) {
56
		$sql = 'SELECT name FROM *PREFIX*face_recognition WHERE uid = ? AND encoding IS NOT NULL GROUP BY name ORDER BY COUNT(name) DESC';
57
		return $this->findEntities($sql, [$userId]);
58
	}
59
60
	public function findNewFile($userId, $fileId) {
61
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE uid = ? AND file = ? AND distance = -1 AND encoding IS NULL';
62
		return $this->findEntities($sql, [$userId, $fileId]);
63
	}
64
65
	public function findFile($userId, $fileId) {
66
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE uid = ? AND file = ?';
67
		return $this->findEntities($sql, [$userId, $fileId]);
68
	}
69
70
	public function fileExists($fileId) {
71
		$sql = 'SELECT * FROM *PREFIX*face_recognition WHERE file = ?';
72
		try {
73
			$faces = $this->findEntities($sql, [$fileId]);
74
			return ($faces != NULL);
75
		} catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type OCA\FaceRecognition\Db\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
76
			return false;
77
		}
78
		return true;
0 ignored issues
show
Unused Code introduced by
return true is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
79
	}
80
81
	public function countUserQueue($userId) {
82
		$sql = 'SELECT count(*) AS ct FROM *PREFIX*face_recognition WHERE uid = ? AND distance = -1 AND encoding IS NULL';
83
		$query = \OCP\DB::prepare($sql);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB::prepare() has been deprecated: 8.1.0 use prepare() of \OCP\IDBConnection - \OC::$server->getDatabaseConnection() ( Ignorable by Annotation )

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

83
		$query = /** @scrutinizer ignore-deprecated */ \OCP\DB::prepare($sql);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
84
		$result = $query->execute([$userId]);
85
		if ($row = $result->fetchRow()) {
86
			return $row['ct'];
87
		}
88
		return 0;
89
	}
90
91
	public function countQueue() {
92
		$sql = 'SELECT count(*) AS ct FROM *PREFIX*face_recognition WHERE distance = -1 AND encoding IS NULL';
93
		$query = \OCP\DB::prepare($sql);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB::prepare() has been deprecated: 8.1.0 use prepare() of \OCP\IDBConnection - \OC::$server->getDatabaseConnection() ( Ignorable by Annotation )

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

93
		$query = /** @scrutinizer ignore-deprecated */ \OCP\DB::prepare($sql);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
94
		$result = $query->execute();
95
		if ($row = $result->fetchRow()) {
96
			return $row['ct'];
97
		}
98
		return 0;
99
	}
100
101
}