From f04a2ce19f07a4b63adcfa6111bba4ea66f8cd72 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 4 Mar 2021 12:29:49 +0100 Subject: [PATCH] Pimp ActionProviderStore * Fix paradoxale condition * Add types * Move away from ILogger Makes psalm happy, and thus me happy Signed-off-by: Roeland Jago Douma --- .../ContactsMenu/ActionProviderStore.php | 33 +++++++++---------- .../ContactsMenu/ActionProviderStoreTest.php | 6 ++-- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/private/Contacts/ContactsMenu/ActionProviderStore.php b/lib/private/Contacts/ContactsMenu/ActionProviderStore.php index 5513dd0636224..10b83adf92dfb 100644 --- a/lib/private/Contacts/ContactsMenu/ActionProviderStore.php +++ b/lib/private/Contacts/ContactsMenu/ActionProviderStore.php @@ -1,4 +1,6 @@ * @@ -28,9 +30,9 @@ use OC\Contacts\ContactsMenu\Providers\EMailProvider; use OCP\AppFramework\QueryException; use OCP\Contacts\ContactsMenu\IProvider; -use OCP\ILogger; use OCP\IServerContainer; use OCP\IUser; +use Psr\Log\LoggerInterface; class ActionProviderStore { @@ -40,15 +42,10 @@ class ActionProviderStore { /** @var AppManager */ private $appManager; - /** @var ILogger */ + /** @var LoggerInterface */ private $logger; - /** - * @param IServerContainer $serverContainer - * @param AppManager $appManager - * @param ILogger $logger - */ - public function __construct(IServerContainer $serverContainer, AppManager $appManager, ILogger $logger) { + public function __construct(IServerContainer $serverContainer, AppManager $appManager, LoggerInterface $logger) { $this->serverContainer = $serverContainer; $this->appManager = $appManager; $this->logger = $logger; @@ -59,7 +56,7 @@ public function __construct(IServerContainer $serverContainer, AppManager $appMa * @return IProvider[] * @throws Exception */ - public function getProviders(IUser $user) { + public function getProviders(IUser $user): array { $appClasses = $this->getAppProviderClasses($user); $providerClasses = $this->getServerProviderClasses(); $allClasses = array_merge($providerClasses, $appClasses); @@ -69,11 +66,13 @@ public function getProviders(IUser $user) { try { $providers[] = $this->serverContainer->query($class); } catch (QueryException $ex) { - $this->logger->logException($ex, [ - 'message' => "Could not load contacts menu action provider $class", - 'app' => 'core', - ]); - throw new Exception("Could not load contacts menu action provider"); + $this->logger->error('Could not load contacts menu action provider ' . $class, + [ + 'app' => 'core', + 'exception' => $ex, + ] + ); + throw new Exception('Could not load contacts menu action provider'); } } @@ -83,7 +82,7 @@ public function getProviders(IUser $user) { /** * @return string[] */ - private function getServerProviderClasses() { + private function getServerProviderClasses(): array { return [ EMailProvider::class, ]; @@ -93,11 +92,11 @@ private function getServerProviderClasses() { * @param IUser $user * @return string[] */ - private function getAppProviderClasses(IUser $user) { + private function getAppProviderClasses(IUser $user): array { return array_reduce($this->appManager->getEnabledAppsForUser($user), function ($all, $appId) { $info = $this->appManager->getAppInfo($appId); - if (!isset($info['contactsmenu']) || !isset($info['contactsmenu'])) { + if (!isset($info['contactsmenu'])) { // Nothing to add return $all; } diff --git a/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php b/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php index f20df96f7a2a9..301c55518f654 100644 --- a/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php +++ b/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php @@ -30,9 +30,9 @@ use OCP\App\IAppManager; use OCP\AppFramework\QueryException; use OCP\Contacts\ContactsMenu\IProvider; -use OCP\ILogger; use OCP\IServerContainer; use OCP\IUser; +use Psr\Log\LoggerInterface; use Test\TestCase; class ActionProviderStoreTest extends TestCase { @@ -43,7 +43,7 @@ class ActionProviderStoreTest extends TestCase { /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */ private $appManager; - /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */ + /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ private $logger; /** @var ActionProviderStore */ @@ -54,7 +54,7 @@ protected function setUp(): void { $this->serverContainer = $this->createMock(IServerContainer::class); $this->appManager = $this->createMock(AppManager::class); - $this->logger = $this->createMock(ILogger::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->actionProviderStore = new ActionProviderStore($this->serverContainer, $this->appManager, $this->logger); }