Completed
Branch master (8ca3ab)
by Sander
03:07
created

VaultMapper::updateVault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
namespace OCA\Passman\Db;
25
26
use OCA\Passman\Utility\Utils;
27
use OCP\IDBConnection;
28
use OCP\AppFramework\Db\Mapper;
29
30
class VaultMapper extends Mapper {
31
	private $utils;
32
	public function __construct(IDBConnection $db, Utils $utils) {
33
		parent::__construct($db, 'passman_vaults');
34
		$this->utils = $utils;
35
	}
36
37
38
	/**
39
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
40
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
41
	 * @return Vault[]
42
	 */
43
	public function find($vault_id, $user_id) {
44
		$sql = 'SELECT * FROM `*PREFIX*passman_vaults` ' .
45
			'WHERE `id`= ? and `user_id` = ?';
46
		return $this->findEntities($sql, [$vault_id, $user_id]);
47
	}
48
	/**
49
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
50
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
51
	 * @return Vault
52
	 */
53 1
	public function findByGuid($vault_guid, $user_id) {
54
		$sql = 'SELECT * FROM `*PREFIX*passman_vaults` ' .
55 1
			'WHERE `guid`= ? and `user_id` = ?';
56 1
		return $this->findEntity($sql, [$vault_guid, $user_id]);
57
	}
58
59
60
	/**
61
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
62
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
63
	 * @return Vault[]
64
	 */
65 1
	public function findVaultsFromUser($userId){
66
		$sql = 'SELECT * FROM `*PREFIX*passman_vaults` ' .
67 1
			'WHERE `user_id` = ? ';
68 1
		$params = [$userId];
69 1
		return $this->findEntities($sql, $params);
70
	}
71
72
	/**
73
	 * Creates a vault
74
	 * @param $vault_name
75
	 * @param $userId
76
	 * @return Vault
77
	 */
78 1
	public function create($vault_name, $userId){
79 1
		$vault = new Vault();
80 1
		$vault->setName($vault_name);
81 1
		$vault->setUserId($userId);
82 1
		$vault->setGuid($this->utils->GUID());
83 1
		$vault->setCreated($this->utils->getTime());
84 1
		$vault->setLastAccess(0);
85 1
		return parent::insert($vault);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (insert() instead of create()). Are you sure this is correct? If so, you might want to change this to $this->insert().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
86
	}
87
88
	/**
89
	 * Update last access time of a vault
90
	 * @param $vault_id
91
	 * @param $user_id
92
	 */
93 1
	public function setLastAccess($vault_id, $user_id){
94 1
		$vault = new Vault();
95 1
		$vault->setId($vault_id);
96 1
		$vault->setUserId($user_id);
97 1
		$vault->setLastAccess(Utils::getTime());
98 1
		$this->update($vault);
99 1
	}
100
101
	/**
102
	 * Update vault
103
	 * @param Vault $vault
104
	 */
105 1
	public function updateVault(Vault $vault){
106 1
		$this->update($vault);
107 1
	}
108
109
	/**
110
	 * Update the sharing key's
111
	 * @param $vault_id
112
	 * @param $privateKey
113
	 * @param $publicKey
114
	 */
115 1
	public function updateSharingKeys($vault_id, $privateKey, $publicKey){
116 1
		$vault = new Vault();
117 1
		$vault->setId($vault_id);
118 1
		$vault->setPrivateSharingKey($privateKey);
119 1
		$vault->setPublicSharingKey($publicKey);
120 1
		$vault->setSharingKeysGenerated($this->utils->getTime());
121 1
		$this->update($vault);
122
	}
123
}