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