You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OCMProvider.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023, Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\OCM\Model;
  25. use JsonSerializable;
  26. use OCP\OCM\Exceptions\OCMArgumentException;
  27. use OCP\OCM\Exceptions\OCMProviderException;
  28. use OCP\OCM\IOCMProvider;
  29. /**
  30. * @since 28.0.0
  31. */
  32. class OCMProvider implements IOCMProvider, JsonSerializable {
  33. private bool $enabled = false;
  34. private string $apiVersion = '';
  35. private string $endPoint = '';
  36. /** @var OCMResource[] */
  37. private array $resourceTypes = [];
  38. /**
  39. * @param bool $enabled
  40. *
  41. * @return OCMProvider
  42. */
  43. public function setEnabled(bool $enabled): self {
  44. $this->enabled = $enabled;
  45. return $this;
  46. }
  47. /**
  48. * @return bool
  49. */
  50. public function isEnabled(): bool {
  51. return $this->enabled;
  52. }
  53. /**
  54. * @param string $apiVersion
  55. *
  56. * @return OCMProvider
  57. */
  58. public function setApiVersion(string $apiVersion): self {
  59. $this->apiVersion = $apiVersion;
  60. return $this;
  61. }
  62. /**
  63. * @return string
  64. */
  65. public function getApiVersion(): string {
  66. return $this->apiVersion;
  67. }
  68. /**
  69. * @param string $endPoint
  70. *
  71. * @return OCMProvider
  72. */
  73. public function setEndPoint(string $endPoint): self {
  74. $this->endPoint = $endPoint;
  75. return $this;
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function getEndPoint(): string {
  81. return $this->endPoint;
  82. }
  83. /**
  84. * @param OCMResource $resource
  85. *
  86. * @return $this
  87. */
  88. public function addResourceType(OCMResource $resource): self {
  89. $this->resourceTypes[] = $resource;
  90. return $this;
  91. }
  92. /**
  93. * @param OCMResource[] $resourceTypes
  94. *
  95. * @return OCMProvider
  96. */
  97. public function setResourceTypes(array $resourceTypes): self {
  98. $this->resourceTypes = $resourceTypes;
  99. return $this;
  100. }
  101. /**
  102. * @return OCMResource[]
  103. */
  104. public function getResourceTypes(): array {
  105. return $this->resourceTypes;
  106. }
  107. /**
  108. * @param string $resourceName
  109. * @param string $protocol
  110. *
  111. * @return string
  112. * @throws OCMArgumentException
  113. */
  114. public function extractProtocolEntry(string $resourceName, string $protocol): string {
  115. foreach ($this->getResourceTypes() as $resource) {
  116. if ($resource->getName() === $resourceName) {
  117. $entry = $resource->getProtocols()[$protocol] ?? null;
  118. if (is_null($entry)) {
  119. throw new OCMArgumentException('protocol not found');
  120. }
  121. return (string)$entry;
  122. }
  123. }
  124. throw new OCMArgumentException('resource not found');
  125. }
  126. /**
  127. * import data from an array
  128. *
  129. * @param array $data
  130. *
  131. * @return self
  132. * @throws OCMProviderException in case a descent provider cannot be generated from data
  133. * @see self::jsonSerialize()
  134. */
  135. public function import(array $data): self {
  136. $this->setEnabled(is_bool($data['enabled'] ?? '') ? $data['enabled'] : false)
  137. ->setApiVersion((string)($data['apiVersion'] ?? ''))
  138. ->setEndPoint($data['endPoint'] ?? '');
  139. $resources = [];
  140. foreach (($data['resourceTypes'] ?? []) as $resourceData) {
  141. $resource = new OCMResource();
  142. $resources[] = $resource->import($resourceData);
  143. }
  144. $this->setResourceTypes($resources);
  145. if (!$this->looksValid()) {
  146. throw new OCMProviderException('remote provider does not look valid');
  147. }
  148. return $this;
  149. }
  150. /**
  151. * @return bool
  152. */
  153. private function looksValid(): bool {
  154. return ($this->getApiVersion() !== '' && $this->getEndPoint() !== '');
  155. }
  156. /**
  157. * @return array{
  158. * enabled: bool,
  159. * apiVersion: string,
  160. * endPoint: string,
  161. * resourceTypes: array{
  162. * name: string,
  163. * shareTypes: string[],
  164. * protocols: array<string, string>
  165. * }[]
  166. * }
  167. */
  168. public function jsonSerialize(): array {
  169. $resourceTypes = []; // this is needed for psalm
  170. foreach ($this->getResourceTypes() as $res) {
  171. $resourceTypes[] = $res->jsonSerialize();
  172. }
  173. return [
  174. 'enabled' => $this->isEnabled(),
  175. 'apiVersion' => $this->getApiVersion(),
  176. 'endPoint' => $this->getEndPoint(),
  177. 'resourceTypes' => $resourceTypes
  178. ];
  179. }
  180. }