OCMProvider.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 OCP\EventDispatcher\IEventDispatcher;
  26. use OCP\OCM\Events\ResourceTypeRegisterEvent;
  27. use OCP\OCM\Exceptions\OCMArgumentException;
  28. use OCP\OCM\Exceptions\OCMProviderException;
  29. use OCP\OCM\IOCMProvider;
  30. use OCP\OCM\IOCMResource;
  31. /**
  32. * @since 28.0.0
  33. */
  34. class OCMProvider implements IOCMProvider {
  35. private bool $enabled = false;
  36. private string $apiVersion = '';
  37. private string $endPoint = '';
  38. /** @var IOCMResource[] */
  39. private array $resourceTypes = [];
  40. private bool $emittedEvent = false;
  41. public function __construct(
  42. protected IEventDispatcher $dispatcher,
  43. ) {
  44. }
  45. /**
  46. * @param bool $enabled
  47. *
  48. * @return $this
  49. */
  50. public function setEnabled(bool $enabled): static {
  51. $this->enabled = $enabled;
  52. return $this;
  53. }
  54. /**
  55. * @return bool
  56. */
  57. public function isEnabled(): bool {
  58. return $this->enabled;
  59. }
  60. /**
  61. * @param string $apiVersion
  62. *
  63. * @return $this
  64. */
  65. public function setApiVersion(string $apiVersion): static {
  66. $this->apiVersion = $apiVersion;
  67. return $this;
  68. }
  69. /**
  70. * @return string
  71. */
  72. public function getApiVersion(): string {
  73. return $this->apiVersion;
  74. }
  75. /**
  76. * @param string $endPoint
  77. *
  78. * @return $this
  79. */
  80. public function setEndPoint(string $endPoint): static {
  81. $this->endPoint = $endPoint;
  82. return $this;
  83. }
  84. /**
  85. * @return string
  86. */
  87. public function getEndPoint(): string {
  88. return $this->endPoint;
  89. }
  90. /**
  91. * create a new resource to later add it with {@see IOCMProvider::addResourceType()}
  92. * @return IOCMResource
  93. */
  94. public function createNewResourceType(): IOCMResource {
  95. return new OCMResource();
  96. }
  97. /**
  98. * @param IOCMResource $resource
  99. *
  100. * @return $this
  101. */
  102. public function addResourceType(IOCMResource $resource): static {
  103. $this->resourceTypes[] = $resource;
  104. return $this;
  105. }
  106. /**
  107. * @param IOCMResource[] $resourceTypes
  108. *
  109. * @return $this
  110. */
  111. public function setResourceTypes(array $resourceTypes): static {
  112. $this->resourceTypes = $resourceTypes;
  113. return $this;
  114. }
  115. /**
  116. * @return IOCMResource[]
  117. */
  118. public function getResourceTypes(): array {
  119. if (!$this->emittedEvent) {
  120. $this->emittedEvent = true;
  121. $event = new ResourceTypeRegisterEvent($this);
  122. $this->dispatcher->dispatchTyped($event);
  123. }
  124. return $this->resourceTypes;
  125. }
  126. /**
  127. * @param string $resourceName
  128. * @param string $protocol
  129. *
  130. * @return string
  131. * @throws OCMArgumentException
  132. */
  133. public function extractProtocolEntry(string $resourceName, string $protocol): string {
  134. foreach ($this->getResourceTypes() as $resource) {
  135. if ($resource->getName() === $resourceName) {
  136. $entry = $resource->getProtocols()[$protocol] ?? null;
  137. if (is_null($entry)) {
  138. throw new OCMArgumentException('protocol not found');
  139. }
  140. return (string)$entry;
  141. }
  142. }
  143. throw new OCMArgumentException('resource not found');
  144. }
  145. /**
  146. * import data from an array
  147. *
  148. * @param array $data
  149. *
  150. * @return $this
  151. * @throws OCMProviderException in case a descent provider cannot be generated from data
  152. * @see self::jsonSerialize()
  153. */
  154. public function import(array $data): static {
  155. $this->setEnabled(is_bool($data['enabled'] ?? '') ? $data['enabled'] : false)
  156. ->setApiVersion((string)($data['apiVersion'] ?? ''))
  157. ->setEndPoint($data['endPoint'] ?? '');
  158. $resources = [];
  159. foreach (($data['resourceTypes'] ?? []) as $resourceData) {
  160. $resource = new OCMResource();
  161. $resources[] = $resource->import($resourceData);
  162. }
  163. $this->setResourceTypes($resources);
  164. if (!$this->looksValid()) {
  165. throw new OCMProviderException('remote provider does not look valid');
  166. }
  167. return $this;
  168. }
  169. /**
  170. * @return bool
  171. */
  172. private function looksValid(): bool {
  173. return ($this->getApiVersion() !== '' && $this->getEndPoint() !== '');
  174. }
  175. /**
  176. * @return array{
  177. * enabled: bool,
  178. * apiVersion: string,
  179. * endPoint: string,
  180. * resourceTypes: array{
  181. * name: string,
  182. * shareTypes: string[],
  183. * protocols: array<string, string>
  184. * }[]
  185. * }
  186. */
  187. public function jsonSerialize(): array {
  188. $resourceTypes = [];
  189. foreach ($this->getResourceTypes() as $res) {
  190. $resourceTypes[] = $res->jsonSerialize();
  191. }
  192. return [
  193. 'enabled' => $this->isEnabled(),
  194. 'apiVersion' => $this->getApiVersion(),
  195. 'endPoint' => $this->getEndPoint(),
  196. 'resourceTypes' => $resourceTypes
  197. ];
  198. }
  199. }