1
0

ActionProviderStore.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Contacts\ContactsMenu;
  8. use Exception;
  9. use OC\App\AppManager;
  10. use OC\Contacts\ContactsMenu\Providers\EMailProvider;
  11. use OC\Contacts\ContactsMenu\Providers\LocalTimeProvider;
  12. use OC\Contacts\ContactsMenu\Providers\ProfileProvider;
  13. use OCP\AppFramework\QueryException;
  14. use OCP\Contacts\ContactsMenu\IBulkProvider;
  15. use OCP\Contacts\ContactsMenu\IProvider;
  16. use OCP\IServerContainer;
  17. use OCP\IUser;
  18. use Psr\Log\LoggerInterface;
  19. class ActionProviderStore {
  20. public function __construct(
  21. private IServerContainer $serverContainer,
  22. private AppManager $appManager,
  23. private LoggerInterface $logger,
  24. ) {
  25. }
  26. /**
  27. * @return list<IProvider|IBulkProvider>
  28. * @throws Exception
  29. */
  30. public function getProviders(IUser $user): array {
  31. $appClasses = $this->getAppProviderClasses($user);
  32. $providerClasses = $this->getServerProviderClasses();
  33. $allClasses = array_merge($providerClasses, $appClasses);
  34. /** @var list<IProvider|IBulkProvider> $providers */
  35. $providers = [];
  36. foreach ($allClasses as $class) {
  37. try {
  38. $provider = $this->serverContainer->get($class);
  39. if ($provider instanceof IProvider || $provider instanceof IBulkProvider) {
  40. $providers[] = $provider;
  41. } else {
  42. $this->logger->warning('Ignoring invalid contacts menu provider', [
  43. 'class' => $class,
  44. ]);
  45. }
  46. } catch (QueryException $ex) {
  47. $this->logger->error(
  48. 'Could not load contacts menu action provider ' . $class,
  49. [
  50. 'app' => 'core',
  51. 'exception' => $ex,
  52. ]
  53. );
  54. throw new Exception('Could not load contacts menu action provider');
  55. }
  56. }
  57. return $providers;
  58. }
  59. /**
  60. * @return string[]
  61. */
  62. private function getServerProviderClasses(): array {
  63. return [
  64. ProfileProvider::class,
  65. LocalTimeProvider::class,
  66. EMailProvider::class,
  67. ];
  68. }
  69. /**
  70. * @return string[]
  71. */
  72. private function getAppProviderClasses(IUser $user): array {
  73. return array_reduce($this->appManager->getEnabledAppsForUser($user), function ($all, $appId) {
  74. $info = $this->appManager->getAppInfo($appId);
  75. if (!isset($info['contactsmenu'])) {
  76. // Nothing to add
  77. return $all;
  78. }
  79. $providers = array_reduce($info['contactsmenu'], function ($all, $provider) {
  80. return array_merge($all, [$provider]);
  81. }, []);
  82. return array_merge($all, $providers);
  83. }, []);
  84. }
  85. }