PropPatch.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV;
  4. use UnexpectedValueException;
  5. /**
  6. * This class represents a set of properties that are going to be updated.
  7. *
  8. * Usually this is simply a PROPPATCH request, but it can also be used for
  9. * internal updates.
  10. *
  11. * Property updates must always be atomic. This means that a property update
  12. * must either completely succeed, or completely fail.
  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. */
  18. class PropPatch
  19. {
  20. /**
  21. * Properties that are being updated.
  22. *
  23. * This is a key-value list. If the value is null, the property is supposed
  24. * to be deleted.
  25. *
  26. * @var array
  27. */
  28. protected $mutations;
  29. /**
  30. * A list of properties and the result of the update. The result is in the
  31. * form of a HTTP status code.
  32. *
  33. * @var array
  34. */
  35. protected $result = [];
  36. /**
  37. * This is the list of callbacks when we're performing the actual update.
  38. *
  39. * @var array
  40. */
  41. protected $propertyUpdateCallbacks = [];
  42. /**
  43. * This property will be set to true if the operation failed.
  44. *
  45. * @var bool
  46. */
  47. protected $failed = false;
  48. /**
  49. * Constructor.
  50. *
  51. * @param array $mutations A list of updates
  52. */
  53. public function __construct(array $mutations)
  54. {
  55. $this->mutations = $mutations;
  56. }
  57. /**
  58. * Call this function if you wish to handle updating certain properties.
  59. * For instance, your class may be responsible for handling updates for the
  60. * {DAV:}displayname property.
  61. *
  62. * In that case, call this method with the first argument
  63. * "{DAV:}displayname" and a second argument that's a method that does the
  64. * actual updating.
  65. *
  66. * It's possible to specify more than one property as an array.
  67. *
  68. * The callback must return a boolean or an it. If the result is true, the
  69. * operation was considered successful. If it's false, it's consided
  70. * failed.
  71. *
  72. * If the result is an integer, we'll use that integer as the http status
  73. * code associated with the operation.
  74. *
  75. * @param string|string[] $properties
  76. */
  77. public function handle($properties, callable $callback)
  78. {
  79. $usedProperties = [];
  80. foreach ((array) $properties as $propertyName) {
  81. if (array_key_exists($propertyName, $this->mutations) && !isset($this->result[$propertyName])) {
  82. $usedProperties[] = $propertyName;
  83. // HTTP Accepted
  84. $this->result[$propertyName] = 202;
  85. }
  86. }
  87. // Only registering if there's any unhandled properties.
  88. if (!$usedProperties) {
  89. return;
  90. }
  91. $this->propertyUpdateCallbacks[] = [
  92. // If the original argument to this method was a string, we need
  93. // to also make sure that it stays that way, so the commit function
  94. // knows how to format the arguments to the callback.
  95. is_string($properties) ? $properties : $usedProperties,
  96. $callback,
  97. ];
  98. }
  99. /**
  100. * Call this function if you wish to handle _all_ properties that haven't
  101. * been handled by anything else yet. Note that you effectively claim with
  102. * this that you promise to process _all_ properties that are coming in.
  103. */
  104. public function handleRemaining(callable $callback)
  105. {
  106. $properties = $this->getRemainingMutations();
  107. if (!$properties) {
  108. // Nothing to do, don't register callback
  109. return;
  110. }
  111. foreach ($properties as $propertyName) {
  112. // HTTP Accepted
  113. $this->result[$propertyName] = 202;
  114. $this->propertyUpdateCallbacks[] = [
  115. $properties,
  116. $callback,
  117. ];
  118. }
  119. }
  120. /**
  121. * Sets the result code for one or more properties.
  122. *
  123. * @param string|string[] $properties
  124. * @param int $resultCode
  125. */
  126. public function setResultCode($properties, $resultCode)
  127. {
  128. foreach ((array) $properties as $propertyName) {
  129. $this->result[$propertyName] = $resultCode;
  130. }
  131. if ($resultCode >= 400) {
  132. $this->failed = true;
  133. }
  134. }
  135. /**
  136. * Sets the result code for all properties that did not have a result yet.
  137. *
  138. * @param int $resultCode
  139. */
  140. public function setRemainingResultCode($resultCode)
  141. {
  142. $this->setResultCode(
  143. $this->getRemainingMutations(),
  144. $resultCode
  145. );
  146. }
  147. /**
  148. * Returns the list of properties that don't have a result code yet.
  149. *
  150. * This method returns a list of property names, but not its values.
  151. *
  152. * @return string[]
  153. */
  154. public function getRemainingMutations()
  155. {
  156. $remaining = [];
  157. foreach ($this->mutations as $propertyName => $propValue) {
  158. if (!isset($this->result[$propertyName])) {
  159. $remaining[] = $propertyName;
  160. }
  161. }
  162. return $remaining;
  163. }
  164. /**
  165. * Returns the list of properties that don't have a result code yet.
  166. *
  167. * This method returns list of properties and their values.
  168. *
  169. * @return array
  170. */
  171. public function getRemainingValues()
  172. {
  173. $remaining = [];
  174. foreach ($this->mutations as $propertyName => $propValue) {
  175. if (!isset($this->result[$propertyName])) {
  176. $remaining[$propertyName] = $propValue;
  177. }
  178. }
  179. return $remaining;
  180. }
  181. /**
  182. * Performs the actual update, and calls all callbacks.
  183. *
  184. * This method returns true or false depending on if the operation was
  185. * successful.
  186. *
  187. * @return bool
  188. */
  189. public function commit()
  190. {
  191. // First we validate if every property has a handler
  192. foreach ($this->mutations as $propertyName => $value) {
  193. if (!isset($this->result[$propertyName])) {
  194. $this->failed = true;
  195. $this->result[$propertyName] = 403;
  196. }
  197. }
  198. foreach ($this->propertyUpdateCallbacks as $callbackInfo) {
  199. if ($this->failed) {
  200. break;
  201. }
  202. if (is_string($callbackInfo[0])) {
  203. $this->doCallbackSingleProp($callbackInfo[0], $callbackInfo[1]);
  204. } else {
  205. $this->doCallbackMultiProp($callbackInfo[0], $callbackInfo[1]);
  206. }
  207. }
  208. /*
  209. * If anywhere in this operation updating a property failed, we must
  210. * update all other properties accordingly.
  211. */
  212. if ($this->failed) {
  213. foreach ($this->result as $propertyName => $status) {
  214. if (202 === $status) {
  215. // Failed dependency
  216. $this->result[$propertyName] = 424;
  217. }
  218. }
  219. }
  220. return !$this->failed;
  221. }
  222. /**
  223. * Executes a property callback with the single-property syntax.
  224. *
  225. * @param string $propertyName
  226. */
  227. private function doCallBackSingleProp($propertyName, callable $callback)
  228. {
  229. $result = $callback($this->mutations[$propertyName]);
  230. if (is_bool($result)) {
  231. if ($result) {
  232. if (is_null($this->mutations[$propertyName])) {
  233. // Delete
  234. $result = 204;
  235. } else {
  236. // Update
  237. $result = 200;
  238. }
  239. } else {
  240. // Fail
  241. $result = 403;
  242. }
  243. }
  244. if (!is_int($result)) {
  245. throw new UnexpectedValueException('A callback sent to handle() did not return an int or a bool');
  246. }
  247. $this->result[$propertyName] = $result;
  248. if ($result >= 400) {
  249. $this->failed = true;
  250. }
  251. }
  252. /**
  253. * Executes a property callback with the multi-property syntax.
  254. */
  255. private function doCallBackMultiProp(array $propertyList, callable $callback)
  256. {
  257. $argument = [];
  258. foreach ($propertyList as $propertyName) {
  259. $argument[$propertyName] = $this->mutations[$propertyName];
  260. }
  261. $result = $callback($argument);
  262. if (is_array($result)) {
  263. foreach ($propertyList as $propertyName) {
  264. if (!isset($result[$propertyName])) {
  265. $resultCode = 500;
  266. } else {
  267. $resultCode = $result[$propertyName];
  268. }
  269. if ($resultCode >= 400) {
  270. $this->failed = true;
  271. }
  272. $this->result[$propertyName] = $resultCode;
  273. }
  274. } elseif (true === $result) {
  275. // Success
  276. foreach ($argument as $propertyName => $propertyValue) {
  277. $this->result[$propertyName] = is_null($propertyValue) ? 204 : 200;
  278. }
  279. } elseif (false === $result) {
  280. // Fail :(
  281. $this->failed = true;
  282. foreach ($propertyList as $propertyName) {
  283. $this->result[$propertyName] = 403;
  284. }
  285. } else {
  286. throw new UnexpectedValueException('A callback sent to handle() did not return an array or a bool');
  287. }
  288. }
  289. /**
  290. * Returns the result of the operation.
  291. *
  292. * @return array
  293. */
  294. public function getResult()
  295. {
  296. return $this->result;
  297. }
  298. /**
  299. * Returns the full list of mutations.
  300. *
  301. * @return array
  302. */
  303. public function getMutations()
  304. {
  305. return $this->mutations;
  306. }
  307. }