ActionProviderStore.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Contacts\ContactsMenu;
  26. use Exception;
  27. use OC\App\AppManager;
  28. use OC\Contacts\ContactsMenu\Providers\EMailProvider;
  29. use OC\Contacts\ContactsMenu\Providers\LocalTimeProvider;
  30. use OC\Contacts\ContactsMenu\Providers\ProfileProvider;
  31. use OCP\AppFramework\QueryException;
  32. use OCP\Contacts\ContactsMenu\IBulkProvider;
  33. use OCP\Contacts\ContactsMenu\IProvider;
  34. use OCP\IServerContainer;
  35. use OCP\IUser;
  36. use Psr\Log\LoggerInterface;
  37. class ActionProviderStore {
  38. public function __construct(
  39. private IServerContainer $serverContainer,
  40. private AppManager $appManager,
  41. private LoggerInterface $logger,
  42. ) {
  43. }
  44. /**
  45. * @return list<IProvider|IBulkProvider>
  46. * @throws Exception
  47. */
  48. public function getProviders(IUser $user): array {
  49. $appClasses = $this->getAppProviderClasses($user);
  50. $providerClasses = $this->getServerProviderClasses();
  51. $allClasses = array_merge($providerClasses, $appClasses);
  52. /** @var list<IProvider|IBulkProvider> $providers */
  53. $providers = [];
  54. foreach ($allClasses as $class) {
  55. try {
  56. $provider = $this->serverContainer->get($class);
  57. if ($provider instanceof IProvider || $provider instanceof IBulkProvider) {
  58. $providers[] = $provider;
  59. } else {
  60. $this->logger->warning('Ignoring invalid contacts menu provider', [
  61. 'class' => $class,
  62. ]);
  63. }
  64. } catch (QueryException $ex) {
  65. $this->logger->error(
  66. 'Could not load contacts menu action provider ' . $class,
  67. [
  68. 'app' => 'core',
  69. 'exception' => $ex,
  70. ]
  71. );
  72. throw new Exception('Could not load contacts menu action provider');
  73. }
  74. }
  75. return $providers;
  76. }
  77. /**
  78. * @return string[]
  79. */
  80. private function getServerProviderClasses(): array {
  81. return [
  82. ProfileProvider::class,
  83. LocalTimeProvider::class,
  84. EMailProvider::class,
  85. ];
  86. }
  87. /**
  88. * @return string[]
  89. */
  90. private function getAppProviderClasses(IUser $user): array {
  91. return array_reduce($this->appManager->getEnabledAppsForUser($user), function ($all, $appId) {
  92. $info = $this->appManager->getAppInfo($appId);
  93. if (!isset($info['contactsmenu'])) {
  94. // Nothing to add
  95. return $all;
  96. }
  97. $providers = array_reduce($info['contactsmenu'], function ($all, $provider) {
  98. return array_merge($all, [$provider]);
  99. }, []);
  100. return array_merge($all, $providers);
  101. }, []);
  102. }
  103. }