ActionProviderStore.php 3.0 KB

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