1
0

ActionProviderStore.php 3.1 KB

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