PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Sabre/DAV/Sync/MockSyncCollection.php

https://github.com/KOLANICH/SabreDAV
PHP | 169 lines | 66 code | 28 blank | 75 comment | 11 complexity | 76473218132c68537166a2a47692d667 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\DAV\Sync;
  3. use Sabre\DAV;
  4. /**
  5. * This mocks a ISyncCollection, for unittesting.
  6. *
  7. * This object behaves the same as SimpleCollection. Call addChange to update
  8. * the 'changelog' that this class uses for the collection.
  9. *
  10. * @copyright Copyright (C) 2007-2013 Rooftop Solutions. All rights reserved.
  11. * @author Evert Pot (http://evertpot.com/)
  12. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  13. */
  14. class MockSyncCollection extends DAV\SimpleCollection implements ISyncCollection {
  15. public $changeLog = [];
  16. public $token = null;
  17. /**
  18. * This method returns the current sync-token for this collection.
  19. * This can be any string.
  20. *
  21. * If null is returned from this function, the plugin assumes there's no
  22. * sync information available.
  23. *
  24. * @return string|null
  25. */
  26. public function getSyncToken() {
  27. // Will be 'null' in the first round, and will increment ever after.
  28. return $this->token;
  29. }
  30. public function addChange(array $added, array $modified, array $deleted) {
  31. $this->token++;
  32. $this->changeLog[$this->token] = [
  33. 'added' => $added,
  34. 'modified' => $modified,
  35. 'deleted' => $deleted,
  36. ];
  37. }
  38. /**
  39. * The getChanges method returns all the changes that have happened, since
  40. * the specified syncToken and the current collection.
  41. *
  42. * This function should return an array, such as the following:
  43. *
  44. * array(
  45. * 'syncToken' => 'The current synctoken',
  46. * 'modified' => array(
  47. * 'new.txt',
  48. * ),
  49. * 'deleted' => array(
  50. * 'foo.php.bak',
  51. * 'old.txt'
  52. * )
  53. * );
  54. *
  55. * The syncToken property should reflect the *current* syncToken of the
  56. * collection, as reported getSyncToken(). This is needed here too, to
  57. * ensure the operation is atomic.
  58. *
  59. * If the syncToken is specified as null, this is an initial sync, and all
  60. * members should be reported.
  61. *
  62. * The modified property is an array of nodenames that have changed since
  63. * the last token.
  64. *
  65. * The deleted property is an array with nodenames, that have been deleted
  66. * from collection.
  67. *
  68. * The second argument is basically the 'depth' of the report. If it's 1,
  69. * you only have to report changes that happened only directly in immediate
  70. * descendants. If it's 2, it should also include changes from the nodes
  71. * below the child collections. (grandchildren)
  72. *
  73. * The third (optional) argument allows a client to specify how many
  74. * results should be returned at most. If the limit is not specified, it
  75. * should be treated as infinite.
  76. *
  77. * If the limit (infinite or not) is higher than you're willing to return,
  78. * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception.
  79. *
  80. * If the syncToken is expired (due to data cleanup) or unknown, you must
  81. * return null.
  82. *
  83. * The limit is 'suggestive'. You are free to ignore it.
  84. *
  85. * @param string $syncToken
  86. * @param int $syncLevel
  87. * @param int $limit
  88. * @return array
  89. */
  90. public function getChanges($syncToken, $syncLevel, $limit = null) {
  91. // This is an initial sync
  92. if (is_null($syncToken)) {
  93. return [
  94. 'added' => array_map(
  95. function($item) {
  96. return $item->getName();
  97. }, $this->getChildren()
  98. ),
  99. 'modified' => [],
  100. 'deleted' => [],
  101. 'syncToken' => $this->getSyncToken(),
  102. ];
  103. }
  104. if (!is_int($syncToken) && !ctype_digit($syncToken)) {
  105. return null;
  106. }
  107. if (is_null($this->token)) return null;
  108. $added = [];
  109. $modified = [];
  110. $deleted = [];
  111. foreach($this->changeLog as $token=>$change) {
  112. if ($token > $syncToken) {
  113. $added = array_merge($added, $change['added']);
  114. $modified = array_merge($modified, $change['modified']);
  115. $deleted = array_merge($deleted, $change['deleted']);
  116. if ($limit) {
  117. // If there's a limit, we may need to cut things off.
  118. // This alghorithm is weird and stupid, but it works.
  119. $left = $limit - (count($modified) + count($deleted));
  120. if ($left>0) continue;
  121. if ($left===0) break;
  122. if ($left<0) {
  123. $modified = array_slice($modified, 0, $left);
  124. }
  125. $left = $limit - (count($modified) + count($deleted));
  126. if ($left===0) break;
  127. if ($left<0) {
  128. $deleted = array_slice($deleted, 0, $left);
  129. }
  130. break;
  131. }
  132. }
  133. }
  134. return array(
  135. 'syncToken' => $this->token,
  136. 'added' => $added,
  137. 'modified' => $modified,
  138. 'deleted' => $deleted,
  139. );
  140. }
  141. }