Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commit 8297b24

Browse files
Spomkyscheb
authored andcommitted
NullLogger added
1 parent ebbd21a commit 8297b24

2 files changed

Lines changed: 9 additions & 15 deletions

File tree

Security/Http/Firewall/TwoFactorListener.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Scheb\TwoFactorBundle\Security\Http\Firewall;
66

77
use Psr\Log\LoggerInterface;
8+
use Psr\Log\NullLogger;
89
use Scheb\TwoFactorBundle\DependencyInjection\Factory\Security\TwoFactorFactory;
910
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenFactoryInterface;
1011
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
@@ -140,7 +141,7 @@ public function __construct(
140141
$this->twoFactorAccessDecider = $twoFactorAccessDecider;
141142
$this->eventDispatcher = $eventDispatcher;
142143
$this->twoFactorTokenFactory = $twoFactorTokenFactory;
143-
$this->logger = $logger;
144+
$this->logger = $logger === null ? new NullLogger() : $logger;
144145
$this->trustedDeviceManager = $trustedDeviceManager;
145146
}
146147

@@ -215,19 +216,15 @@ private function attemptAuthentication(Request $request, TwoFactorTokenInterface
215216

216217
private function onFailure(Request $request, AuthenticationException $failed): Response
217218
{
218-
if ($this->logger) {
219-
$this->logger->info('Two-factor authentication request failed.', ['exception' => $failed]);
220-
}
219+
$this->logger->info('Two-factor authentication request failed.', ['exception' => $failed]);
221220
$this->dispatchTwoFactorAuthenticationEvent(TwoFactorAuthenticationEvents::FAILURE, $request, $this->tokenStorage->getToken());
222221

223222
return $this->failureHandler->onAuthenticationFailure($request, $failed);
224223
}
225224

226225
private function onSuccess(Request $request, TokenInterface $token, TwoFactorTokenInterface $previousTwoFactorToken): Response
227226
{
228-
if ($this->logger) {
229-
$this->logger->info('User has been two-factor authenticated successfully.', ['username' => $token->getUsername()]);
230-
}
227+
$this->logger->info('User has been two-factor authenticated successfully.', ['username' => $token->getUsername()]);
231228
$this->tokenStorage->setToken($token);
232229
$this->dispatchTwoFactorAuthenticationEvent(TwoFactorAuthenticationEvents::SUCCESS, $request, $token);
233230

Security/TwoFactor/Provider/TwoFactorProviderPreparationListener.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Provider;
66

77
use Psr\Log\LoggerInterface;
8+
use Psr\Log\NullLogger;
89
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
910
use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvent;
1011
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
@@ -29,7 +30,7 @@ class TwoFactorProviderPreparationListener
2930
private $twoFactorToken;
3031

3132
/**
32-
* @var LoggerInterface|null
33+
* @var LoggerInterface
3334
*/
3435
private $logger;
3536

@@ -58,7 +59,7 @@ public function __construct(
5859
) {
5960
$this->providerRegistry = $providerRegistry;
6061
$this->preparationRecorder = $preparationRecorder;
61-
$this->logger = $logger;
62+
$this->logger = $logger === null ? new NullLogger() : $logger;
6263
$this->firewallName = $firewallName;
6364
$this->prepareOnLogin = $prepareOnLogin;
6465
$this->prepareOnAccessDenied = $prepareOnAccessDenied;
@@ -105,18 +106,14 @@ public function onKernelFinishRequest(FinishRequestEvent $event): void
105106

106107
try {
107108
if ($this->preparationRecorder->isProviderPrepared($firewallName, $providerName)) {
108-
if ($this->logger) {
109-
$this->logger->info(sprintf('Two-factor provider "%s" was already prepared.', $providerName));
110-
}
109+
$this->logger->info(sprintf('Two-factor provider "%s" was already prepared.', $providerName));
111110

112111
return;
113112
}
114113
$user = $this->twoFactorToken->getUser();
115114
$this->providerRegistry->getProvider($providerName)->prepareAuthentication($user);
116115
$this->preparationRecorder->recordProviderIsPrepared($firewallName, $providerName);
117-
if ($this->logger) {
118-
$this->logger->info(sprintf('Two-factor provider "%s" prepared.', $providerName));
119-
}
116+
$this->logger->info(sprintf('Two-factor provider "%s" prepared.', $providerName));
120117
} finally {
121118
$this->preparationRecorder->saveSession();
122119
}

0 commit comments

Comments
 (0)