Completed
Push — phpunit ( 286476...52c072 )
by Marcos
10:21
created

ShareRequestMapper::createRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 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
    public function createRequest(ShareRequest $request){
26
        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
    public function getRequestByItemAndVaultGuid($item_guid, $target_vault_guid){
36
        $q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_guid = ? AND target_vault_guid = ?";
37
        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
    public function getRequestsByItemGuidGroupedByUser($item_guid){
46
        $this->db->executeQuery("SET sql_mode = '';");
47
        $q = "SELECT *, target_user_id FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_guid = ? GROUP BY target_user_id;";
48
        return $this->findEntities($q, [$item_guid]);
49
    }
50
51
    /**
52
     * Deletes all pending requests for the given user to the given item
53
     * @param $item_id          The item ID
54
     * @param $target_user_id   The target user
55
     * @return \PDOStatement    The result of running the db query
56
     */
57
    public function cleanItemRequestsForUser($item_id, $target_user_id){
58
		$q = "DELETE FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_id = ? AND target_user_id = ?";
59
		$this->execute($q, [$item_id, $target_user_id]);
60
        return $this->execute($q, [$item_id, $target_user_id]);
61
    }
62
63
    /**
64
     * Obtains all pending share requests for the given user ID
65
     * @param $user_id
66
     * @return ShareRequest[]
67
     */
68
    public function getUserPendingRequests($user_id){
69
        $q = "SELECT * FROM *PREFIX*". self::TABLE_NAME ." WHERE target_user_id = ?";
70
        return $this->findEntities($q, [$user_id]);
71
    }
72
73
    /**
74
     * Deletes the given share request
75
     * @param ShareRequest $shareRequest    Request to delete
76
     * @return ShareRequest                 The deleted request
77
     */
78
    public function deleteShareRequest(ShareRequest $shareRequest){
79
    	return $this->delete($shareRequest);
80
	}
81
82
    /**
83
     * Gets a share request by it's unique incremental id
84
     * @param $id
85
     * @return ShareRequest
86
	 * @throws DoesNotExistException
87
     */
88
	public function getShareRequestById($id){
89
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE id = ?";
90
		return $this->findEntity($q, [$id]);
91
	}
92
93
    /**
94
     * Gets all share requests by a given item GUID
95
     * @param $item_guid
96
     * @return ShareRequest[]
97
     */
98
	public function getShareRequestsByItemGuid($item_guid){
99
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE 	item_guid = ?";
100
		return $this->findEntities($q, [$item_guid]);
101
	}
102
103
    /**
104
     * Updates the given share request,
105
     * @param ShareRequest $shareRequest
106
     * @return ShareRequest
107
     */
108
	public function updateShareRequest(ShareRequest $shareRequest){
109
		return $this->update($shareRequest);
110
	}
111
112
    /**
113
     * Finds pending requests sent to the given user to the given item.
114
     * @param $item_guid
115
     * @param $user_id
116
     * @return ShareRequest[]
117
     */
118
	public function getPendingShareRequests($item_guid, $user_id){
119
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE 	item_guid = ? and target_user_id= ?";
120
		return $this->findEntities($q, [$item_guid, $user_id]);
121
	}
122
123
    /**
124
     * Updates all pending requests with the given permissions
125
     * @param $item_guid        The item for which to update the requests
126
     * @param $user_id          The user for which to update the requests
127
     * @param $permissions      The new permissions to apply
128
     * @return \PDOStatement    The result of the operation
129
     */
130
	public function updatePendinRequestPermissions($item_guid, $user_id, $permissions){
131
	    $q = "UPDATE *PREFIX*" . self::TABLE_NAME . " SET permissions = ? WHERE item_guid = ? AND target_user_id = ?";
132
        return $this->execute($q, [$permissions, $item_guid, $user_id]);
133
    }
134
135
}