PublicShareTemplateFactory.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Share20;
  8. use Exception;
  9. use OC\AppFramework\Bootstrap\Coordinator;
  10. use OCA\Files_Sharing\DefaultPublicShareTemplateProvider;
  11. use OCP\Server;
  12. use OCP\Share\IPublicShareTemplateFactory;
  13. use OCP\Share\IPublicShareTemplateProvider;
  14. use OCP\Share\IShare;
  15. class PublicShareTemplateFactory implements IPublicShareTemplateFactory {
  16. public function __construct(
  17. private Coordinator $coordinator,
  18. private DefaultPublicShareTemplateProvider $defaultProvider,
  19. ) {
  20. }
  21. public function getProvider(IShare $share): IPublicShareTemplateProvider {
  22. $context = $this->coordinator->getRegistrationContext();
  23. if ($context === null) {
  24. throw new Exception("Can't retrieve public share template providers as context is not defined");
  25. }
  26. $providers = array_map(
  27. fn ($registration) => Server::get($registration->getService()),
  28. $context->getPublicShareTemplateProviders()
  29. );
  30. $filteredProviders = array_filter(
  31. $providers,
  32. fn (IPublicShareTemplateProvider $provider) => $provider->shouldRespond($share)
  33. );
  34. if (count($filteredProviders) === 0) {
  35. return $this->defaultProvider;
  36. } else {
  37. return array_shift($filteredProviders);
  38. }
  39. }
  40. }