Merge pull request #25902 from nextcloud/techdept/psalm/loggerinterface/part2

LoggerInterface for provisioning API Controllers
This commit is contained in:
Roeland Jago Douma 2021-03-03 12:53:15 +01:00 committed by GitHub
commit b87e54bb1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 43 deletions

View File

@ -42,16 +42,16 @@ use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use Psr\Log\LoggerInterface;
class GroupsController extends AUserData {
/** @var ILogger */
/** @var LoggerInterface */
private $logger;
public function __construct(string $appName,
@ -62,7 +62,7 @@ class GroupsController extends AUserData {
IUserSession $userSession,
AccountManager $accountManager,
IFactory $l10nFactory,
ILogger $logger) {
LoggerInterface $logger) {
parent::__construct($appName,
$request,
$userManager,

View File

@ -60,7 +60,6 @@ use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
@ -70,6 +69,7 @@ use OCP\L10N\IFactory;
use OCP\Security\ISecureRandom;
use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\EventDispatcher\IEventDispatcher;
use Psr\Log\LoggerInterface;
class UsersController extends AUserData {
@ -77,7 +77,7 @@ class UsersController extends AUserData {
private $appManager;
/** @var IURLGenerator */
protected $urlGenerator;
/** @var ILogger */
/** @var LoggerInterface */
private $logger;
/** @var IFactory */
protected $l10nFactory;
@ -101,7 +101,7 @@ class UsersController extends AUserData {
IUserSession $userSession,
AccountManager $accountManager,
IURLGenerator $urlGenerator,
ILogger $logger,
LoggerInterface $logger,
IFactory $l10nFactory,
NewUserMailHelper $newUserMailHelper,
FederatedShareProviderFactory $federatedShareProviderFactory,
@ -417,36 +417,40 @@ class UsersController extends AUserData {
} catch (\Exception $e) {
// Mail could be failing hard or just be plain not configured
// Logging error as it is the hardest of the two
$this->logger->logException($e, [
'message' => "Unable to send the invitation mail to $email",
'level' => ILogger::ERROR,
'app' => 'ocs_api',
]);
$this->logger->error("Unable to send the invitation mail to $email",
[
'app' => 'ocs_api',
'exception' => $e,
]
);
}
}
}
return new DataResponse(['id' => $userid]);
} catch (HintException $e) {
$this->logger->logException($e, [
'message' => 'Failed addUser attempt with hint exception.',
'level' => ILogger::WARN,
'app' => 'ocs_api',
]);
$this->logger->warning('Failed addUser attempt with hint exception.',
[
'app' => 'ocs_api',
'exception' => $e,
]
);
throw new OCSException($e->getHint(), 107);
} catch (OCSException $e) {
$this->logger->logException($e, [
'message' => 'Failed addUser attempt with ocs exeption.',
'level' => ILogger::ERROR,
'app' => 'ocs_api',
]);
$this->logger->warning('Failed addUser attempt with ocs exeption.',
[
'app' => 'ocs_api',
'exception' => $e,
]
);
throw $e;
} catch (\Exception $e) {
$this->logger->logException($e, [
'message' => 'Failed addUser attempt with exception.',
'level' => ILogger::ERROR,
'app' => 'ocs_api',
]);
$this->logger->error('Failed addUser attempt with exception.',
[
'app' => 'ocs_api',
'exception' => $e
]
);
throw new OCSException('Bad request', 101);
}
}
@ -1047,11 +1051,12 @@ class UsersController extends AUserData {
$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
$this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
} catch (\Exception $e) {
$this->logger->logException($e, [
'message' => "Can't send new user mail to $email",
'level' => ILogger::ERROR,
'app' => 'settings',
]);
$this->logger->error("Can't send new user mail to $email",
[
'app' => 'settings',
'exception' => $e,
]
);
throw new OCSException('Sending email failed', 102);
}

View File

@ -37,13 +37,13 @@ use OC\User\NoUserException;
use OCA\Provisioning_API\Controller\GroupsController;
use OCP\Accounts\IAccountManager;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\UserInterface;
use Psr\Log\LoggerInterface;
class GroupsControllerTest extends \Test\TestCase {
@ -59,7 +59,7 @@ class GroupsControllerTest extends \Test\TestCase {
protected $userSession;
/** @var AccountManager|\PHPUnit\Framework\MockObject\MockObject */
protected $accountManager;
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
protected $logger;
/** @var SubAdmin|\PHPUnit\Framework\MockObject\MockObject */
protected $subAdminManager;
@ -78,7 +78,7 @@ class GroupsControllerTest extends \Test\TestCase {
$this->userSession = $this->createMock(IUserSession::class);
$this->accountManager = $this->createMock(AccountManager::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->logger = $this->createMock(ILogger::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->subAdminManager = $this->createMock(SubAdmin::class);

View File

@ -56,7 +56,6 @@ use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
@ -68,6 +67,7 @@ use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\Security\ISecureRandom;
use OCP\UserInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class UsersControllerTest extends TestCase {
@ -82,7 +82,7 @@ class UsersControllerTest extends TestCase {
protected $groupManager;
/** @var IUserSession|MockObject */
protected $userSession;
/** @var ILogger|MockObject */
/** @var LoggerInterface|MockObject */
protected $logger;
/** @var UsersController|MockObject */
protected $api;
@ -113,7 +113,7 @@ class UsersControllerTest extends TestCase {
$this->appManager = $this->createMock(IAppManager::class);
$this->groupManager = $this->createMock(Manager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->logger = $this->createMock(ILogger::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->request = $this->createMock(IRequest::class);
$this->accountManager = $this->createMock(AccountManager::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
@ -715,12 +715,13 @@ class UsersControllerTest extends TestCase {
->will($this->throwException($exception));
$this->logger
->expects($this->once())
->method('logException')
->with($exception, [
'message' => 'Failed addUser attempt with exception.',
'level' => ILogger::ERROR,
'app' => 'ocs_api',
]);
->method('error')
->with('Failed addUser attempt with exception.',
[
'app' => 'ocs_api',
'exception' => $exception
]
);
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();