AssetMapper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 6
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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
32 View Code Duplication
class AssetMapper 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...
33
	/** @var int Limetime of a token is 10 minutes */
34
	const tokenLifeTime = 600;
35
36
	/** @var ISecureRandom */
37
	private $random;
38
39
	/** @var ITimeFactory */
40
	private $time;
41
42
	public function __construct(IDBConnection $db, ISecureRandom $random, ITimeFactory $timeFactory) {
43
		parent::__construct($db, 'richdocuments_assets', Asset::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...
44
45
		$this->random = $random;
46
		$this->time = $timeFactory;
47
	}
48
49
	/**
50
	 * @param $uid
51
	 * @param $fileid
52
	 * @return Asset
53
	 */
54
	public function newAsset($uid, $fileid) {
55
		$asset = new Asset();
56
		$asset->setUid($uid);
57
		$asset->setFileid($fileid);
58
		$asset->setTimestamp($this->time->getTime());
59
		$asset->setToken($this->random->generate(64, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS));
60
61
		$asset = $this->insert($asset);
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...
62
		return $asset;
63
	}
64
65
66
	/**
67
	 * @param $token
68
	 * @return Asset
69
	 * @throws DoesNotExistException
70
	 */
71
	public function getAssetByToken($token) {
72
		$qb = $this->db->getQueryBuilder();
73
		$qb->select('*')
74
			->from('richdocuments_assets')
75
			->where($qb->expr()->eq('token', $qb->createNamedParameter($token)));
76
77
		$cursor = $qb->execute();
78
		$data = $cursor->fetch();
79
		$cursor->closeCursor();
80
81
		//There can only be one row since it is a unqiue field
82
		if ($data === false) {
83
			throw new DoesNotExistException('No asset for token found');
84
		}
85
86
		$asset = Asset::fromRow($data);
87
88
		// Check the token lifetime
89
		if ($asset->getTimestamp() + self::tokenLifeTime < $this->time->getTime()) {
90
			$this->delete($asset);
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...
91
			throw new DoesNotExistException('No asset for token found');
92
		}
93
94
		return $asset;
95
	}
96
}
97