DirectMapper::getByToken()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 24
loc 24
ccs 0
cts 18
cp 0
rs 9.536
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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