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