123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- declare(strict_types=1);
- namespace OC\OCM\Model;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\OCM\Events\ResourceTypeRegisterEvent;
- use OCP\OCM\Exceptions\OCMArgumentException;
- use OCP\OCM\Exceptions\OCMProviderException;
- use OCP\OCM\IOCMProvider;
- use OCP\OCM\IOCMResource;
- class OCMProvider implements IOCMProvider {
- private bool $enabled = false;
- private string $apiVersion = '';
- private string $endPoint = '';
-
- private array $resourceTypes = [];
- private bool $emittedEvent = false;
- public function __construct(
- protected IEventDispatcher $dispatcher,
- ) {
- }
-
- public function setEnabled(bool $enabled): static {
- $this->enabled = $enabled;
- return $this;
- }
-
- public function isEnabled(): bool {
- return $this->enabled;
- }
-
- public function setApiVersion(string $apiVersion): static {
- $this->apiVersion = $apiVersion;
- return $this;
- }
-
- public function getApiVersion(): string {
- return $this->apiVersion;
- }
-
- public function setEndPoint(string $endPoint): static {
- $this->endPoint = $endPoint;
- return $this;
- }
-
- public function getEndPoint(): string {
- return $this->endPoint;
- }
-
- public function createNewResourceType(): IOCMResource {
- return new OCMResource();
- }
-
- public function addResourceType(IOCMResource $resource): static {
- $this->resourceTypes[] = $resource;
- return $this;
- }
-
- public function setResourceTypes(array $resourceTypes): static {
- $this->resourceTypes = $resourceTypes;
- return $this;
- }
-
- public function getResourceTypes(): array {
- if (!$this->emittedEvent) {
- $this->emittedEvent = true;
- $event = new ResourceTypeRegisterEvent($this);
- $this->dispatcher->dispatchTyped($event);
- }
- return $this->resourceTypes;
- }
-
- public function extractProtocolEntry(string $resourceName, string $protocol): string {
- foreach ($this->getResourceTypes() as $resource) {
- if ($resource->getName() === $resourceName) {
- $entry = $resource->getProtocols()[$protocol] ?? null;
- if (is_null($entry)) {
- throw new OCMArgumentException('protocol not found');
- }
- return (string)$entry;
- }
- }
- throw new OCMArgumentException('resource not found');
- }
-
- public function import(array $data): static {
- $this->setEnabled(is_bool($data['enabled'] ?? '') ? $data['enabled'] : false)
- ->setApiVersion((string)($data['apiVersion'] ?? ''))
- ->setEndPoint($data['endPoint'] ?? '');
- $resources = [];
- foreach (($data['resourceTypes'] ?? []) as $resourceData) {
- $resource = new OCMResource();
- $resources[] = $resource->import($resourceData);
- }
- $this->setResourceTypes($resources);
- if (!$this->looksValid()) {
- throw new OCMProviderException('remote provider does not look valid');
- }
- return $this;
- }
-
- private function looksValid(): bool {
- return ($this->getApiVersion() !== '' && $this->getEndPoint() !== '');
- }
-
- public function jsonSerialize(): array {
- $resourceTypes = [];
- foreach ($this->getResourceTypes() as $res) {
- $resourceTypes[] = $res->jsonSerialize();
- }
- return [
- 'enabled' => $this->isEnabled(),
- 'apiVersion' => $this->getApiVersion(),
- 'endPoint' => $this->getEndPoint(),
- 'resourceTypes' => $resourceTypes
- ];
- }
- }
|