Failed Conditions
Pull Request — master (#682)
by
unknown
09:03
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 File
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
		/** @var File $file */
63
		$file = $this->findEntity($qb);
64
		return $file;
65
	}
66
67
	/**
68
	 * @param string $file_guid
69
	 * @param string|null $user_id
70
	 * @return File
71
	 * @throws DoesNotExistException
72
	 * @throws MultipleObjectsReturnedException
73
	 */
74
	public function getFileByGuid(string $file_guid, string $user_id = null) {
75
		$qb = $this->db->getQueryBuilder();
76
		$qb->select('*')
77
			->from(self::TABLE_NAME)
78
			->where($qb->expr()->eq('guid', $qb->createNamedParameter($file_guid, IQueryBuilder::PARAM_STR)));
79
80
		if ($user_id !== null) {
81
			$qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
82
		}
83
84
		/** @var File $file */
85
		$file = $this->findEntity($qb);
86
		return $file;
87
	}
88
89
	/**
90
	 * @param $file_raw
91
	 * @param $userId
92
	 * @return File
93
	 */
94
	public function create($file_raw, $userId) {
95
		$file = new File();
96
		$file->setGuid($this->utils->GUID());
97
		$file->setUserId($userId);
98
		$file->setFilename($file_raw['filename']);
99
		$file->setSize($file_raw['size']);
100
		$file->setCreated($this->utils->getTime());
101
		$file->setFileData($file_raw['file_data']);
102
		$file->setMimetype($file_raw['mimetype']);
103
104
		return $this->insert($file);
105
	}
106
107
	/**
108
	 * Delete a file by file_id and user id
109
	 *
110
	 * @param int $file_id
111
	 * @param string $userId
112
	 * @return File|Entity
113
	 */
114
	public function deleteFile(int $file_id, string $userId) {
115
		$file = new File();
116
		$file->setId($file_id);
117
		$file->setUserId($userId);
118
		return $this->delete($file);
119
	}
120
121
	/**
122
	 * Uodate file
123
	 * @param File $file
124
	 * @return File
125
	 */
126
	public function updateFile(File $file) {
127
		return $this->update($file);
128
	}
129
130
131
	/**
132
	 * @param string $user_id
133
	 * @return File[]
134
	 */
135
	public function getFilesFromUser(string $user_id) {
136
		$qb = $this->db->getQueryBuilder();
137
		$qb->select('*')
138
			->from(self::TABLE_NAME)
139
			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
140
141
		/** @var File[] $files */
142
		$files = $this->findEntities($qb);
143
		return $files;
144
	}
145
}
146