1
0

ActionProviderStore.php 3.0 KB

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