1<?php
2
3namespace Sabre\CalDAV\Backend;
4
5use Sabre\CalDAV;
6use Sabre\DAV;
7
8/**
9 * This is a mock CalDAV backend that supports subscriptions.
10 *
11 * All data is retained in memory temporarily. It's primary purpose is
12 * unit-tests.
13 *
14 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
15 * @author Evert Pot (http://evertpot.com/)
16 * @license http://sabre.io/license/ Modified BSD License
17 */
18class MockSubscriptionSupport extends Mock implements SubscriptionSupport {
19
20    /**
21     * Subscription list
22     *
23     * @var array
24     */
25    protected $subs = [];
26
27    /**
28     * Returns a list of subscriptions for a principal.
29     *
30     * Every subscription is an array with the following keys:
31     *  * id, a unique id that will be used by other functions to modify the
32     *    subscription. This can be the same as the uri or a database key.
33     *  * uri. This is just the 'base uri' or 'filename' of the subscription.
34     *  * principaluri. The owner of the subscription. Almost always the same as
35     *    principalUri passed to this method.
36     *  * source. Url to the actual feed
37     *
38     * Furthermore, all the subscription info must be returned too:
39     *
40     * 1. {DAV:}displayname
41     * 2. {http://apple.com/ns/ical/}refreshrate
42     * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos
43     *    should not be stripped).
44     * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms
45     *    should not be stripped).
46     * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if
47     *    attachments should not be stripped).
48     * 7. {http://apple.com/ns/ical/}calendar-color
49     * 8. {http://apple.com/ns/ical/}calendar-order
50     *
51     * @param string $principalUri
52     * @return array
53     */
54    function getSubscriptionsForUser($principalUri) {
55
56        if (isset($this->subs[$principalUri])) {
57            return $this->subs[$principalUri];
58        }
59        return [];
60
61    }
62
63    /**
64     * Creates a new subscription for a principal.
65     *
66     * If the creation was a success, an id must be returned that can be used to reference
67     * this subscription in other methods, such as updateSubscription.
68     *
69     * @param string $principalUri
70     * @param string $uri
71     * @param array $properties
72     * @return mixed
73     */
74    function createSubscription($principalUri, $uri, array $properties) {
75
76        $properties['uri'] = $uri;
77        $properties['principaluri'] = $principalUri;
78        $properties['source'] = $properties['{http://calendarserver.org/ns/}source']->getHref();
79
80        if (!isset($this->subs[$principalUri])) {
81            $this->subs[$principalUri] = [];
82        }
83
84        $id = [$principalUri, count($this->subs[$principalUri]) + 1];
85
86        $properties['id'] = $id;
87
88        $this->subs[$principalUri][] = array_merge($properties, [
89            'id' => $id,
90        ]);
91
92        return $id;
93
94    }
95
96    /**
97     * Updates a subscription
98     *
99     * The list of mutations is stored in a Sabre\DAV\PropPatch object.
100     * To do the actual updates, you must tell this object which properties
101     * you're going to process with the handle() method.
102     *
103     * Calling the handle method is like telling the PropPatch object "I
104     * promise I can handle updating this property".
105     *
106     * Read the PropPatch documentation for more info and examples.
107     *
108     * @param mixed $subscriptionId
109     * @param \Sabre\DAV\PropPatch $propPatch
110     * @return void
111     */
112    function updateSubscription($subscriptionId, DAV\PropPatch $propPatch) {
113
114        $found = null;
115        foreach ($this->subs[$subscriptionId[0]] as &$sub) {
116
117            if ($sub['id'][1] === $subscriptionId[1]) {
118                $found = & $sub;
119                break;
120            }
121
122        }
123
124        if (!$found) return;
125
126        $propPatch->handleRemaining(function($mutations) use (&$found) {
127            foreach ($mutations as $k => $v) {
128                $found[$k] = $v;
129            }
130            return true;
131        });
132
133    }
134
135    /**
136     * Deletes a subscription
137     *
138     * @param mixed $subscriptionId
139     * @return void
140     */
141    function deleteSubscription($subscriptionId) {
142
143        foreach ($this->subs[$subscriptionId[0]] as $index => $sub) {
144
145            if ($sub['id'][1] === $subscriptionId[1]) {
146                unset($this->subs[$subscriptionId[0]][$index]);
147                return true;
148            }
149
150        }
151
152        return false;
153
154    }
155
156}
157