Failed Conditions
Push — master ( e8410d...a45585 )
by Marcos
09:36 queued 11s
created

lib/Db/FileMapper.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
25
namespace OCA\Passman\Db;
26
27
use OCA\Passman\Utility\Utils;
28
use OCP\AppFramework\Db\DoesNotExistException;
29
use OCP\AppFramework\Db\Entity;
30
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
31
use OCP\AppFramework\Db\QBMapper;
32
use OCP\DB\QueryBuilder\IQueryBuilder;
33
use OCP\IDBConnection;
34
35
class FileMapper extends QBMapper {
36
	const TABLE_NAME = 'passman_files';
37
	private Utils $utils;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
38
39
	public function __construct(IDBConnection $db, Utils $utils) {
40
		parent::__construct($db, self::TABLE_NAME);
41
		$this->utils = $utils;
42
	}
43
44
45
	/**
46
	 * @param int $file_id
47
	 * @param string|null $user_id
48
	 * @return Entity
49
	 * @throws DoesNotExistException
50
	 * @throws MultipleObjectsReturnedException
51
	 */
52
	public function getFile(int $file_id, string $user_id = null) {
53
		$qb = $this->db->getQueryBuilder();
54
		$qb->select('*')
55
			->from(self::TABLE_NAME)
56
			->where($qb->expr()->eq('id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT)));
57
58
		if ($user_id !== null) {
59
			$qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
60
		}
61
62
		return $this->findEntity($qb);
63
	}
64
65
	/**
66
	 * @param string $file_guid
67
	 * @param string|null $user_id
68
	 * @return Entity
69
	 * @throws DoesNotExistException
70
	 * @throws MultipleObjectsReturnedException
71
	 */
72
	public function getFileByGuid(string $file_guid, string $user_id = null) {
73
		$qb = $this->db->getQueryBuilder();
74
		$qb->select('*')
75
			->from(self::TABLE_NAME)
76
			->where($qb->expr()->eq('guid', $qb->createNamedParameter($file_guid, IQueryBuilder::PARAM_STR)));
77
78
		if ($user_id !== null) {
79
			$qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
80
		}
81
82
		return $this->findEntity($qb);
83
	}
84
85
	/**
86
	 * @param $file_raw
87
	 * @param $userId
88
	 * @return File
89
	 */
90
	public function create($file_raw, $userId) {
91
		$file = new File();
92
		$file->setGuid($this->utils->GUID());
93
		$file->setUserId($userId);
94
		$file->setFilename($file_raw['filename']);
95
		$file->setSize($file_raw['size']);
96
		$file->setCreated($this->utils->getTime());
97
		$file->setFileData($file_raw['file_data']);
98
		$file->setMimetype($file_raw['mimetype']);
99
100
		return $this->insert($file);
101
	}
102
103
	/**
104
	 * Delete a file by file_id and user id
105
	 *
106
	 * @param int $file_id
107
	 * @param string $userId
108
	 * @return File|Entity
109
	 */
110
	public function deleteFile(int $file_id, string $userId) {
111
		$file = new File();
112
		$file->setId($file_id);
113
		$file->setUserId($userId);
114
		return $this->delete($file);
115
	}
116
117
	/**
118
	 * Uodate file
119
	 * @param File $file
120
	 * @return File
121
	 */
122
	public function updateFile(File $file) {
123
		return $this->update($file);
124
	}
125
126
127
	/**
128
	 * @param string $user_id
129
	 * @return Entity[]
130
	 */
131
	public function getFilesFromUser(string $user_id) {
132
		$qb = $this->db->getQueryBuilder();
133
		$qb->select('*')
134
			->from(self::TABLE_NAME)
135
			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
136
137
		return $this->findEntities($qb);
138
	}
139
}
140