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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
<?php
namespace Sabre\DAV;
use UnexpectedValueException;
/**
* This class represents a set of properties that are going to be updated.
*
* Usually this is simply a PROPPATCH request, but it can also be used for
* internal updates.
*
* Property updates must always be atomic. This means that a property update
* must either completely succeed, or completely fail.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class PropPatch {
/**
* Properties that are being updated.
*
* This is a key-value list. If the value is null, the property is supposed
* to be deleted.
*
* @var array
*/
protected $mutations;
/**
* A list of properties and the result of the update. The result is in the
* form of a HTTP status code.
*
* @var array
*/
protected $result = [];
/**
* This is the list of callbacks when we're performing the actual update.
*
* @var array
*/
protected $propertyUpdateCallbacks = [];
/**
* This property will be set to true if the operation failed.
*
* @var bool
*/
protected $failed = false;
/**
* Constructor
*
* @param array $mutations A list of updates
*/
function __construct(array $mutations) {
$this->mutations = $mutations;
}
/**
* Call this function if you wish to handle updating certain properties.
* For instance, your class may be responsible for handling updates for the
* {DAV:}displayname property.
*
* In that case, call this method with the first argument
* "{DAV:}displayname" and a second argument that's a method that does the
* actual updating.
*
* It's possible to specify more than one property as an array.
*
* The callback must return a boolean or an it. If the result is true, the
* operation was considered successful. If it's false, it's consided
* failed.
*
* If the result is an integer, we'll use that integer as the http status
* code associated with the operation.
*
* @param string|string[] $properties
* @param callable $callback
* @return void
*/
function handle($properties, callable $callback) {
$usedProperties = [];
foreach ((array)$properties as $propertyName) {
if (array_key_exists($propertyName, $this->mutations) && !isset($this->result[$propertyName])) {
$usedProperties[] = $propertyName;
// HTTP Accepted
$this->result[$propertyName] = 202;
}
}
// Only registering if there's any unhandled properties.
if (!$usedProperties) {
return;
}
$this->propertyUpdateCallbacks[] = [
// If the original argument to this method was a string, we need
// to also make sure that it stays that way, so the commit function
// knows how to format the arguments to the callback.
is_string($properties) ? $properties : $usedProperties,
$callback
];
}
/**
* Call this function if you wish to handle _all_ properties that haven't
* been handled by anything else yet. Note that you effectively claim with
* this that you promise to process _all_ properties that are coming in.
*
* @param callable $callback
* @return void
*/
function handleRemaining(callable $callback) {
$properties = $this->getRemainingMutations();
if (!$properties) {
// Nothing to do, don't register callback
return;
}
foreach ($properties as $propertyName) {
// HTTP Accepted
$this->result[$propertyName] = 202;
$this->propertyUpdateCallbacks[] = [
$properties,
$callback
];
}
}
/**
* Sets the result code for one or more properties.
*
* @param string|string[] $properties
* @param int $resultCode
* @return void
*/
function setResultCode($properties, $resultCode) {
foreach ((array)$properties as $propertyName) {
$this->result[$propertyName] = $resultCode;
}
if ($resultCode >= 400) {
$this->failed = true;
}
}
/**
* Sets the result code for all properties that did not have a result yet.
*
* @param int $resultCode
* @return void
*/
function setRemainingResultCode($resultCode) {
$this->setResultCode(
$this->getRemainingMutations(),
$resultCode
);
}
/**
* Returns the list of properties that don't have a result code yet.
*
* This method returns a list of property names, but not its values.
*
* @return string[]
*/
function getRemainingMutations() {
$remaining = [];
foreach ($this->mutations as $propertyName => $propValue) {
if (!isset($this->result[$propertyName])) {
$remaining[] = $propertyName;
}
}
return $remaining;
}
/**
* Returns the list of properties that don't have a result code yet.
*
* This method returns list of properties and their values.
*
* @return array
*/
function getRemainingValues() {
$remaining = [];
foreach ($this->mutations as $propertyName => $propValue) {
if (!isset($this->result[$propertyName])) {
$remaining[$propertyName] = $propValue;
}
}
return $remaining;
}
/**
* Performs the actual update, and calls all callbacks.
*
* This method returns true or false depending on if the operation was
* successful.
*
* @return bool
*/
function commit() {
// First we validate if every property has a handler
foreach ($this->mutations as $propertyName => $value) {
if (!isset($this->result[$propertyName])) {
$this->failed = true;
$this->result[$propertyName] = 403;
}
}
foreach ($this->propertyUpdateCallbacks as $callbackInfo) {
if ($this->failed) {
break;
}
if (is_string($callbackInfo[0])) {
$this->doCallbackSingleProp($callbackInfo[0], $callbackInfo[1]);
} else {
$this->doCallbackMultiProp($callbackInfo[0], $callbackInfo[1]);
}
}
/**
* If anywhere in this operation updating a property failed, we must
* update all other properties accordingly.
*/
if ($this->failed) {
foreach ($this->result as $propertyName => $status) {
if ($status === 202) {
// Failed dependency
$this->result[$propertyName] = 424;
}
}
}
return !$this->failed;
}
/**
* Executes a property callback with the single-property syntax.
*
* @param string $propertyName
* @param callable $callback
* @return void
*/
private function doCallBackSingleProp($propertyName, callable $callback) {
$result = $callback($this->mutations[$propertyName]);
if (is_bool($result)) {
if ($result) {
if (is_null($this->mutations[$propertyName])) {
// Delete
$result = 204;
} else {
// Update
$result = 200;
}
} else {
// Fail
$result = 403;
}
}
if (!is_int($result)) {
throw new UnexpectedValueException('A callback sent to handle() did not return an int or a bool');
}
$this->result[$propertyName] = $result;
if ($result >= 400) {
$this->failed = true;
}
}
/**
* Executes a property callback with the multi-property syntax.
*
* @param array $propertyList
* @param callable $callback
* @return void
*/
private function doCallBackMultiProp(array $propertyList, callable $callback) {
$argument = [];
foreach ($propertyList as $propertyName) {
$argument[$propertyName] = $this->mutations[$propertyName];
}
$result = $callback($argument);
if (is_array($result)) {
foreach ($propertyList as $propertyName) {
if (!isset($result[$propertyName])) {
$resultCode = 500;
} else {
$resultCode = $result[$propertyName];
}
if ($resultCode >= 400) {
$this->failed = true;
}
$this->result[$propertyName] = $resultCode;
}
} elseif ($result === true) {
// Success
foreach ($argument as $propertyName => $propertyValue) {
$this->result[$propertyName] = is_null($propertyValue) ? 204 : 200;
}
} elseif ($result === false) {
// Fail :(
$this->failed = true;
foreach ($propertyList as $propertyName) {
$this->result[$propertyName] = 403;
}
} else {
throw new UnexpectedValueException('A callback sent to handle() did not return an array or a bool');
}
}
/**
* Returns the result of the operation.
*
* @return array
*/
function getResult() {
return $this->result;
}
/**
* Returns the full list of mutations
*
* @return array
*/
function getMutations() {
return $this->mutations;
}
}
|