DirectMapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 67
loc 67
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A newDirect() 11 11 1
A getByToken() 24 24 3

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
 * @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
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
namespace OCA\Richdocuments\Db;
25
26
use OCP\AppFramework\Db\DoesNotExistException;
27
use OCP\AppFramework\Db\Mapper;
28
use OCP\AppFramework\Utility\ITimeFactory;
29
use OCP\IDBConnection;
30
use OCP\Security\ISecureRandom;
31
use PhpParser\Node\Scalar\MagicConst\Dir;
32
33 View Code Duplication
class DirectMapper extends Mapper {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Deprecated Code introduced by
The class OCP\AppFramework\Db\Mapper has been deprecated with message: 14.0.0 Move over to QBMapper

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
34
35
	/** @var int Limetime of a token is 10 minutes */
36
	const tokenLifeTime = 600;
37
38
	/** @var ISecureRandom */
39
	protected $random;
40
41
	/**@var ITimeFactory */
42
	protected $timeFactory;
43
44
	public function __construct(IDBConnection $db,
45
								ISecureRandom $random,
46
								ITimeFactory $timeFactory) {
47
		parent::__construct($db, 'richdocuments_direct', Direct::class);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::__construct() has been deprecated with message: 14.0.0 Move over to QBMapper

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

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

Loading history...
48
49
		$this->random = $random;
50
		$this->timeFactory = $timeFactory;
51
	}
52
53
	/**
54
	 * @param string $uid
55
	 * @param int $fileid
56
	 * @param int $destination
57
	 * @return Direct
58
	 */
59
	public function newDirect($uid, $fileid, $destination = null) {
60
		$direct = new Direct();
61
		$direct->setUid($uid);
62
		$direct->setFileid($fileid);
63
		$direct->setToken($this->random->generate(64, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER));
64
		$direct->setTimestamp($this->timeFactory->getTime());
65
		$direct->setTemplateDestination($destination);
66
67
		$direct = $this->insert($direct);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::insert() has been deprecated with message: 14.0.0 Move over to QBMapper

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

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

Loading history...
68
		return $direct;
69
	}
70
71
	/**
72
	 * @param string $token
73
	 * @return Direct
74
	 */
75
	public function getByToken($token) {
76
		$qb = $this->db->getQueryBuilder();
77
		$qb->select('*')
78
			->from('richdocuments_direct')
79
			->where($qb->expr()->eq('token', $qb->createNamedParameter($token)));
80
81
		$cursor = $qb->execute();
82
		$row = $cursor->fetch();
83
		$cursor->closeCursor();
84
85
		//There can only be one as the token is unique
86
		if ($row === false) {
87
			throw new DoesNotExistException('Could not find token.');
88
		}
89
90
		$direct = Direct::fromRow($row);
91
92
		if (($direct->getTimestamp() + self::tokenLifeTime) < $this->timeFactory->getTime()) {
93
			$this->delete($direct);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

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

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

Loading history...
94
			throw new DoesNotExistException('Could not find token.');
95
		}
96
97
		return $direct;
98
	}
99
}
100