1<?php
2
3namespace Sabre\CalDAV\Backend;
4
5use Sabre\DAV;
6use Sabre\CalDAV;
7use Sabre\CalDAV\Xml\Notification\NotificationInterface;
8
9class MockSharing extends Mock implements NotificationSupport, SharingSupport {
10
11    private $shares = [];
12    private $notifications;
13
14    function __construct(array $calendars = [], array $calendarData = [], array $notifications = []) {
15
16        parent::__construct($calendars, $calendarData);
17        $this->notifications = $notifications;
18
19    }
20
21    /**
22     * Returns a list of notifications for a given principal url.
23     *
24     * The returned array should only consist of implementations of
25     * Sabre\CalDAV\Notifications\INotificationType.
26     *
27     * @param string $principalUri
28     * @return array
29     */
30    function getNotificationsForPrincipal($principalUri) {
31
32        if (isset($this->notifications[$principalUri])) {
33            return $this->notifications[$principalUri];
34        }
35        return [];
36
37    }
38
39    /**
40     * This deletes a specific notifcation.
41     *
42     * This may be called by a client once it deems a notification handled.
43     *
44     * @param string $principalUri
45     * @param NotificationInterface $notification
46     * @return void
47     */
48    function deleteNotification($principalUri, NotificationInterface $notification) {
49
50        foreach($this->notifications[$principalUri] as $key=>$value) {
51            if ($notification === $value) {
52                unset($this->notifications[$principalUri][$key]);
53            }
54        }
55
56    }
57
58    /**
59     * Updates the list of shares.
60     *
61     * The first array is a list of people that are to be added to the
62     * calendar.
63     *
64     * Every element in the add array has the following properties:
65     *   * href - A url. Usually a mailto: address
66     *   * commonName - Usually a first and last name, or false
67     *   * summary - A description of the share, can also be false
68     *   * readOnly - A boolean value
69     *
70     * Every element in the remove array is just the address string.
71     *
72     * Note that if the calendar is currently marked as 'not shared' by and
73     * this method is called, the calendar should be 'upgraded' to a shared
74     * calendar.
75     *
76     * @param mixed $calendarId
77     * @param array $add
78     * @param array $remove
79     * @return void
80     */
81    function updateShares($calendarId, array $add, array $remove) {
82
83        if (!isset($this->shares[$calendarId])) {
84            $this->shares[$calendarId] = [];
85        }
86
87        foreach($add as $val) {
88            $val['status'] = CalDAV\SharingPlugin::STATUS_NORESPONSE;
89            $this->shares[$calendarId][] = $val;
90        }
91
92        foreach($this->shares[$calendarId] as $k=>$share) {
93
94            if (in_array($share['href'], $remove)) {
95                unset($this->shares[$calendarId][$k]);
96            }
97
98        }
99
100        // Re-numbering keys
101        $this->shares[$calendarId] = array_values($this->shares[$calendarId]);
102
103    }
104
105    /**
106     * Returns the list of people whom this calendar is shared with.
107     *
108     * Every element in this array should have the following properties:
109     *   * href - Often a mailto: address
110     *   * commonName - Optional, for example a first + last name
111     *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
112     *   * readOnly - boolean
113     *   * summary - Optional, a description for the share
114     *
115     * @param mixed $calendarId
116     * @return array
117     */
118    function getShares($calendarId) {
119
120        if (!isset($this->shares[$calendarId])) {
121            return [];
122        }
123
124        return $this->shares[$calendarId];
125
126    }
127
128    /**
129     * This method is called when a user replied to a request to share.
130     *
131     * @param string href The sharee who is replying (often a mailto: address)
132     * @param int status One of the SharingPlugin::STATUS_* constants
133     * @param string $calendarUri The url to the calendar thats being shared
134     * @param string $inReplyTo The unique id this message is a response to
135     * @param string $summary A description of the reply
136     * @return void
137     */
138    function shareReply($href, $status, $calendarUri, $inReplyTo, $summary = null) {
139
140        // This operation basically doesn't do anything yet
141        if ($status === CalDAV\SharingPlugin::STATUS_ACCEPTED) {
142            return 'calendars/blabla/calendar';
143        }
144
145    }
146
147    /**
148     * Publishes a calendar
149     *
150     * @param mixed $calendarId
151     * @param bool $value
152     * @return void
153     */
154    function setPublishStatus($calendarId, $value) {
155
156        foreach($this->calendars as $k=>$cal) {
157            if ($cal['id'] === $calendarId) {
158                if (!$value) {
159                    unset($cal['{http://calendarserver.org/ns/}publish-url']);
160                } else {
161                    $cal['{http://calendarserver.org/ns/}publish-url'] = 'http://example.org/public/ ' . $calendarId . '.ics';
162                }
163                return;
164            }
165        }
166
167        throw new DAV\Exception('Calendar with id "' . $calendarId . '" not found');
168
169    }
170
171}
172
173