Completed
Pull Request — master (#32545)
by Tom
08:55
created

TransferRequestMapper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 9.33 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 7
loc 75
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A findById() 7 7 1
A findRequestWithSameSignature() 0 12 1
A findOpenRequestForGivenFiles() 0 13 1
A findOpenRequestsOlderThan() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author Tom Needham <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
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
namespace OCA\Files\Service\TransferOwnership;
23
24
use OCP\AppFramework\Db\Mapper;
25
use OCP\IDBConnection;
26
27
class TransferRequestMapper extends Mapper {
28
29
	const TABLENAME = 'transfer_requests';
30
31
	public function __construct(IDBConnection $db) {
32
		parent::__construct($db, self::TABLENAME, TransferRequest::class);
33
	}
34
35
	/**
36
	 * @param $requestId
37
	 * @return \OCP\AppFramework\Db\Entity|TransferRequest
38
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
39
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
40
	 */
41 View Code Duplication
	public function findById($requestId) {
42
		$query = $this->db->getQueryBuilder();
43
		$query->select('*')
44
			->from($this->getTableName())
45
			->where($query->expr()->eq('id', $query->expr()->literal($requestId)));
46
		return $this->findEntity($query->getSQL(), $query->getParameters());
47
	}
48
49
	/**
50
	 * Finds a pending transfer for the same file between two users
51
	 * @param $sourceUser
52
	 * @param $destinationUser
53
	 * @param $fileId
54
	 * @return array
55
	 */
56
	public function findRequestWithSameSignature($sourceUser, $destinationUser, $fileId) {
57
		$query = $this->db->getQueryBuilder();
58
		$query->select('*')
59
			->from($this->getTableName())
60
			->where($query->expr()->eq('source_user_id', $query->expr()->literal($sourceUser)))
61
			->andWhere($query->expr()->eq('destination_user_id', $query->expr()->literal($destinationUser)))
62
			->andWhere($query->expr()->eq('file_id', $query->expr()->literal($fileId)))
63
			->andWhere($query->expr()->isNull('rejected_time'))
64
			->andWhere($query->expr()->isNull('accepted_time'))
65
			->andWhere($query->expr()->isNull('actioned_time'));
66
		return $this->findEntities($query->getSQL(), $query->getParameters());
67
	}
68
69
	/**
70
	 * Tries to see if there is an open request for a range of file ids
71
	 * This is used to check if we are trying to make a new request on a file that is already pending transfer
72
	 * @param array $files
73
	 * @return array
74
	 */
75
	public function findOpenRequestForGivenFiles($files) {
76
		$query = $this->db->getQueryBuilder();
77
		$files = array_map(function($fileid) use ($query) {
78
			return $query->expr()->literal($fileid);
79
		}, $files);
80
		$query->select('*')
81
			->from($this->getTableName())
82
			->where($query->expr()->in('file_id', $files))
83
			->andWhere($query->expr()->isNull('rejected_time'))
84
			->andWhere($query->expr()->isNull('accepted_time'))
85
			->andWhere($query->expr()->isNull('actioned_time'));
86
		return $this->findEntities($query->getSQL(), $query->getParameters());
87
	}
88
89
	public function findOpenRequestsOlderThan($days) {
90
		$threshold = \OC::$server->getTimeFactory()->getTime() - 60*60*$days;
91
		$query = $this->db->getQueryBuilder();
92
		$query->select('*')
93
			->from($this->getTableName())
94
			->where($query->expr()->isNull('rejected_time'))
95
			->andWhere($query->expr()->isNull('accepted_time'))
96
			->andWhere($query->expr()->isNull('actioned_time'))
97
			->andWhere($query->expr()->lt('requested_time', $query->expr()->literal($threshold)));
98
		return $this->findEntities($query->getSQL(), $query->getParameters());
99
	}
100
101
}