Completed
Push — master ( dbecc0...75f541 )
by Marcos
02:07 queued 02:01
created

ShareRequestMapper::getRequestsByItemGuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: wolfi
5
 * Date: 1/10/16
6
 * Time: 23:15
7
 */
8
9
namespace OCA\Passman\Db;
10
11
12
use Icewind\SMB\Share;
13
use OCA\Passman\Utility\Utils;
14
use OCP\AppFramework\Db\DoesNotExistException;
15
use OCP\AppFramework\Db\Mapper;
16
use OCP\IDBConnection;
17
18
class ShareRequestMapper extends Mapper {
19
    const TABLE_NAME = 'passman_share_request';
20
21
    public function __construct(IDBConnection $db) {
22
        parent::__construct($db, self::TABLE_NAME);
23
    }
24
25 1
    public function createRequest(ShareRequest $request){
26 1
        return $this->insert($request);
27
    }
28
29
    /**
30
     * Obtains a request by the given item and vault GUID pair
31
     * @param $item_guid
32
     * @param $target_vault_guid
33
     * @return ShareRequest
34
     */
35 1
    public function getRequestByItemAndVaultGuid($item_guid, $target_vault_guid){
36 1
        $q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_guid = ? AND target_vault_guid = ?";
37 1
        return $this->findEntity($q, [$item_guid, $target_vault_guid]);
38
    }
39
40
    /**
41
     * Get shared items for the given item_guid
42
     * @param $item_guid
43
     * @return ShareRequest[]
44
     */
45 1
    public function getRequestsByItemGuidGroupedByUser($item_guid){
46 1
    	if (strtolower($this->db->getDatabasePlatform()->getName()) === 'mysql'){
47 1
    		$this->db->executeQuery("SET sql_mode = '';");
48
		}
49 1
        $q = "SELECT *, target_user_id FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_guid = ? GROUP BY target_user_id;";
50 1
        return $this->findEntities($q, [$item_guid]);
51
    }
52
53
    /**
54
     * Deletes all pending requests for the given user to the given item
55
     * @param $item_id          The item ID
56
     * @param $target_user_id   The target user
57
     * @return \PDOStatement    The result of running the db query
58
     */
59 1
    public function cleanItemRequestsForUser($item_id, $target_user_id){
60 1
		$q = "DELETE FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_id = ? AND target_user_id = ?";
61 1
		$this->execute($q, [$item_id, $target_user_id]);
62 1
        return $this->execute($q, [$item_id, $target_user_id]);
63
    }
64
65
    /**
66
     * Obtains all pending share requests for the given user ID
67
     * @param $user_id
68
     * @return ShareRequest[]
69
     */
70 1
    public function getUserPendingRequests($user_id){
71 1
        $q = "SELECT * FROM *PREFIX*". self::TABLE_NAME ." WHERE target_user_id = ?";
72 1
        return $this->findEntities($q, [$user_id]);
73
    }
74
75
    /**
76
     * Deletes the given share request
77
     * @param ShareRequest $shareRequest    Request to delete
78
     * @return ShareRequest                 The deleted request
79
     */
80 1
    public function deleteShareRequest(ShareRequest $shareRequest){
81 1
    	return $this->delete($shareRequest);
82
	}
83
84
    /**
85
     * Gets a share request by it's unique incremental id
86
     * @param $id
87
     * @return ShareRequest
88
	 * @throws DoesNotExistException
89
     */
90 1
	public function getShareRequestById($id){
91 1
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE id = ?";
92 1
		return $this->findEntity($q, [$id]);
93
	}
94
95
    /**
96
     * Gets all share requests by a given item GUID
97
     * @param $item_guid
98
     * @return ShareRequest[]
99
     */
100 1
	public function getShareRequestsByItemGuid($item_guid){
101 1
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE 	item_guid = ?";
102 1
		return $this->findEntities($q, [$item_guid]);
103
	}
104
105
    /**
106
     * Updates the given share request,
107
     * @param ShareRequest $shareRequest
108
     * @return ShareRequest
109
     */
110 1
	public function updateShareRequest(ShareRequest $shareRequest){
111 1
		return $this->update($shareRequest);
112
	}
113
114
    /**
115
     * Finds pending requests sent to the given user to the given item.
116
     * @param $item_guid
117
     * @param $user_id
118
     * @return ShareRequest[]
119
     */
120 1
	public function getPendingShareRequests($item_guid, $user_id){
121 1
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE 	item_guid = ? and target_user_id= ?";
122 1
		return $this->findEntities($q, [$item_guid, $user_id]);
123
	}
124
125
    /**
126
     * Updates all pending requests with the given permissions
127
     * @param $item_guid        The item for which to update the requests
128
     * @param $user_id          The user for which to update the requests
129
     * @param $permissions      The new permissions to apply
130
     * @return \PDOStatement    The result of the operation
131
     */
132 1
	public function updatePendingRequestPermissions($item_guid, $user_id, $permissions){
133 1
	    $q = "UPDATE *PREFIX*" . self::TABLE_NAME . " SET permissions = ? WHERE item_guid = ? AND target_user_id = ?";
134 1
        return $this->execute($q, [$permissions, $item_guid, $user_id]);
135
    }
136
137
}