-
Notifications
You must be signed in to change notification settings - Fork 343
/
MockSharing.php
195 lines (175 loc) · 6.07 KB
/
MockSharing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
namespace Sabre\CalDAV\Backend;
use Sabre\CalDAV\Xml\Notification\NotificationInterface;
use Sabre\DAV;
class MockSharing extends Mock implements NotificationSupport, SharingSupport
{
private $shares = [];
private $notifications;
public function __construct(array $calendars = [], array $calendarData = [], array $notifications = [])
{
parent::__construct($calendars, $calendarData);
$this->notifications = $notifications;
}
/**
* Returns a list of calendars for a principal.
*
* Every project is an array with the following keys:
* * id, a unique id that will be used by other functions to modify the
* calendar. This can be the same as the uri or a database key.
* * uri, which the basename of the uri with which the calendar is
* accessed.
* * principalUri. The owner of the calendar. Almost always the same as
* principalUri passed to this method.
*
* Furthermore it can contain webdav properties in clark notation. A very
* common one is '{DAV:}displayname'.
*
* @param string $principalUri
*
* @return array
*/
public function getCalendarsForUser($principalUri)
{
$calendars = parent::getCalendarsForUser($principalUri);
foreach ($calendars as $k => $calendar) {
if (isset($calendar['share-access'])) {
continue;
}
if (!empty($this->shares[$calendar['id']])) {
$calendar['share-access'] = DAV\Sharing\Plugin::ACCESS_SHAREDOWNER;
} else {
$calendar['share-access'] = DAV\Sharing\Plugin::ACCESS_NOTSHARED;
}
$calendars[$k] = $calendar;
}
return $calendars;
}
/**
* Returns a list of notifications for a given principal url.
*
* The returned array should only consist of implementations of
* Sabre\CalDAV\Notifications\INotificationType.
*
* @param string $principalUri
*
* @return array
*/
public function getNotificationsForPrincipal($principalUri)
{
if (isset($this->notifications[$principalUri])) {
return $this->notifications[$principalUri];
}
return [];
}
/**
* This deletes a specific notification.
*
* This may be called by a client once it deems a notification handled.
*
* @param string $principalUri
*/
public function deleteNotification($principalUri, NotificationInterface $notification)
{
foreach ($this->notifications[$principalUri] as $key => $value) {
if ($notification === $value) {
unset($this->notifications[$principalUri][$key]);
}
}
}
/**
* Updates the list of shares.
*
* @param mixed $calendarId
* @param \Sabre\DAV\Xml\Element\Sharee[] $sharees
*/
public function updateInvites($calendarId, array $sharees)
{
if (!isset($this->shares[$calendarId])) {
$this->shares[$calendarId] = [];
}
foreach ($sharees as $sharee) {
$existingKey = null;
foreach ($this->shares[$calendarId] as $k => $existingSharee) {
if ($sharee->href === $existingSharee->href) {
$existingKey = $k;
}
}
// Just making sure we're not affecting an existing copy.
$sharee = clone $sharee;
$sharee->inviteStatus = DAV\Sharing\Plugin::INVITE_NORESPONSE;
if (DAV\Sharing\Plugin::ACCESS_NOACCESS === $sharee->access) {
// It's a removal
unset($this->shares[$calendarId][$existingKey]);
} elseif ($existingKey) {
// It's an update
$this->shares[$calendarId][$existingKey] = $sharee;
} else {
// It's an addition
$this->shares[$calendarId][] = $sharee;
}
}
// Re-numbering keys
$this->shares[$calendarId] = array_values($this->shares[$calendarId]);
}
/**
* Returns the list of people whom this calendar is shared with.
*
* Every item in the returned list must be a Sharee object with at
* least the following properties set:
* $href
* $shareAccess
* $inviteStatus
*
* and optionally:
* $properties
*
* @param mixed $calendarId
*
* @return \Sabre\DAV\Xml\Element\Sharee[]
*/
public function getInvites($calendarId)
{
if (!isset($this->shares[$calendarId])) {
return [];
}
return $this->shares[$calendarId];
}
/**
* This method is called when a user replied to a request to share.
*
* @param string href The sharee who is replying (often a mailto: address)
* @param int status One of the \Sabre\DAV\Sharing\Plugin::INVITE_* constants
* @param string $calendarUri The url to the calendar thats being shared
* @param string $inReplyTo The unique id this message is a response to
* @param string $summary A description of the reply
*/
public function shareReply($href, $status, $calendarUri, $inReplyTo, $summary = null)
{
// This operation basically doesn't do anything yet
if (DAV\Sharing\Plugin::INVITE_ACCEPTED === $status) {
return 'calendars/blabla/calendar';
}
}
/**
* Publishes a calendar.
*
* @param mixed $calendarId
* @param bool $value
*/
public function setPublishStatus($calendarId, $value)
{
foreach ($this->calendars as $k => $cal) {
if ($cal['id'] === $calendarId) {
if (!$value) {
unset($cal['{http://calendarserver.org/ns/}publish-url']);
} else {
$cal['{http://calendarserver.org/ns/}publish-url'] = 'http://example.org/public/ '.$calendarId.'.ics';
}
return;
}
}
throw new DAV\Exception('Calendar with id "'.$calendarId.'" not found');
}
}