ActionProviderStore.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\IProvider;
  33. use OCP\IServerContainer;
  34. use OCP\IUser;
  35. use Psr\Log\LoggerInterface;
  36. class ActionProviderStore {
  37. public function __construct(
  38. private IServerContainer $serverContainer,
  39. private AppManager $appManager,
  40. private LoggerInterface $logger,
  41. ) {
  42. }
  43. /**
  44. * @return IProvider[]
  45. * @throws Exception
  46. */
  47. public function getProviders(IUser $user): array {
  48. $appClasses = $this->getAppProviderClasses($user);
  49. $providerClasses = $this->getServerProviderClasses();
  50. $allClasses = array_merge($providerClasses, $appClasses);
  51. $providers = [];
  52. foreach ($allClasses as $class) {
  53. try {
  54. $providers[] = $this->serverContainer->get($class);
  55. } catch (QueryException $ex) {
  56. $this->logger->error(
  57. 'Could not load contacts menu action provider ' . $class,
  58. [
  59. 'app' => 'core',
  60. 'exception' => $ex,
  61. ]
  62. );
  63. throw new Exception('Could not load contacts menu action provider');
  64. }
  65. }
  66. return $providers;
  67. }
  68. /**
  69. * @return string[]
  70. */
  71. private function getServerProviderClasses(): array {
  72. return [
  73. ProfileProvider::class,
  74. LocalTimeProvider::class,
  75. EMailProvider::class,
  76. ];
  77. }
  78. /**
  79. * @return string[]
  80. */
  81. private function getAppProviderClasses(IUser $user): array {
  82. return array_reduce($this->appManager->getEnabledAppsForUser($user), function ($all, $appId) {
  83. $info = $this->appManager->getAppInfo($appId);
  84. if (!isset($info['contactsmenu'])) {
  85. // Nothing to add
  86. return $all;
  87. }
  88. $providers = array_reduce($info['contactsmenu'], function ($all, $provider) {
  89. return array_merge($all, [$provider]);
  90. }, []);
  91. return array_merge($all, $providers);
  92. }, []);
  93. }
  94. }