Completed
Push — master ( f07480...b54a4e )
by Thomas
13s
created

PaperHiveMetadata::insertBookID()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * @author Piotr Mrowczynski <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, Piotr Mrowczynski.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
/**
23
 * This class handles hooks.
24
 */
25
namespace OCA\Files_PaperHive;
26
27
use Doctrine\DBAL\DBALException;
28
use OCP\IDBConnection;
29
use OCP\ILogger;
30
31
class PaperHiveMetadata {
32
33
	/**
34
	 * Database connection
35
	 *
36
	 * @var IDBConnection
37
	 */
38
	private $dbConn;
39
40
	/**
41
	 * Logger
42
	 *
43
	 * @var ILogger
44
	 */
45
	private $logger;
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param IDBConnection $dbConn database connection
51
	 * @param ILogger $logger logger
52
	 */
53
	public function __construct(IDBConnection $dbConn, ILogger $logger) {
54
		$this->dbConn = $dbConn;
55
		$this->logger = $logger;
56
	}
57
58
	/**
59
	 * @param $fileId
60
	 * @param $docId
61
	 * @return bool
62
	 */
63
	public function insertBookID($fileId, $docId) {
64
		try {
65
			$result = $this->dbConn->insertIfNotExist('*PREFIX*paperhive', [
66
				'fileid' => $fileId,
67
				'bookid' => $docId,
68
			]);
69
70
			return ($result === 1);
71
		} catch (DBALException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\DBALException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
72
			$this->logger->logException($e, [
73
				'app' => 'files_paperhive',
74
				'message' => 'Could not add BookID to paperhive table'
75
			]);
76
77
			return false;
78
		}
79
	}
80
81
	/**
82
	 * @param $fileId
83
	 * @return string|null
84
	 */
85
	public function getBookID($fileId) {
86
		$qb = $this->dbConn->getQueryBuilder();
87
		$cursor = $qb->select(['bookid'])
88
			->from('paperhive')
89
			->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId)))
90
			->execute();
91
92
		while ($row = $cursor->fetch()) {
93
			return $row['bookid'];
94
		}
95
		$cursor->closeCursor();
96
97
		return null;
98
	}
99
100
	/**
101
	 * @param $fileId
102
	 * @return bool
103
	 */
104
	public function deleteBookID($fileId) {
105
		$qb = $this->dbConn->getQueryBuilder();
106
		$result = $qb->delete('paperhive')
107
			->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId)))
108
			->execute();
109
110
		return ($result === 1);
111
	}
112
}
113