Skip to content

Commit

Permalink
Replaced Logger::getInstance()->getLogger() with $this->logger for Co…
Browse files Browse the repository at this point in the history
…ntrollers
  • Loading branch information
Alxarafe committed Mar 13, 2019
1 parent 2d1e511 commit 71bcf00
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
11 changes: 5 additions & 6 deletions src/Alxarafe/Core/Base/AuthController.php
Expand Up @@ -8,7 +8,6 @@

use Alxarafe\Core\Models\User;
use Alxarafe\Core\Providers\FlashMessages;
use Alxarafe\Core\Providers\Logger;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -54,9 +53,9 @@ class AuthController extends Controller
public function runMethod(string $methodName): Response
{
$method = $methodName . 'Method';
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('call-to', ['%called%' => $this->shortName . '->' . $method . '()']));
$this->logger->addDebug($this->translator->trans('call-to', ['%called%' => $this->shortName . '->' . $method . '()']));
if (!$this->checkAuth()) {
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('user-not-authenticated'));
$this->logger->addDebug($this->translator->trans('user-not-authenticated'));
return $this->redirect(baseUrl($this->defaultRedirect));
}
return $this->{$method}();
Expand Down Expand Up @@ -85,7 +84,7 @@ private function checkLoginWeb(): bool
if (!empty($username) && !empty($logKey)) {
$user = new User();
if ($user->verifyLogKey($username, $logKey)) {
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('user-logged-in-from-cookie', ['%username%' => $username]));
$this->logger->addDebug($this->translator->trans('user-logged-in-from-cookie', ['%username%' => $username]));
$this->user = $user;
$this->username = $this->user->username;
$this->logkey = $this->user->logkey;
Expand All @@ -112,10 +111,10 @@ private function checkLoginAPI(): bool
if (!empty($userAuth) && !empty($passAuth)) {
$user = new User();
if ($user->getBy('username', $userAuth) && password_verify($passAuth, $user->password)) {
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('api-user-logged', ['%username%' => $userAuth]));
$this->logger->addDebug($this->translator->trans('api-user-logged', ['%username%' => $userAuth]));
$return = true;
} else {
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('api-user-logged-fail', ['%username%' => $userAuth]));
$this->logger->addDebug($this->translator->trans('api-user-logged-fail', ['%username%' => $userAuth]));
}
}
return $return;
Expand Down
17 changes: 12 additions & 5 deletions src/Alxarafe/Core/Base/Controller.php
Expand Up @@ -13,6 +13,7 @@
use Alxarafe\Core\Providers\Logger;
use Alxarafe\Core\Providers\TemplateRender;
use Alxarafe\Core\Providers\Translator;
use Monolog\Logger as MonologLogger;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -74,6 +75,13 @@ abstract class Controller
*/
public $debugTool;

/**
* The logger.
*
* @var MonologLogger
*/
public $logger;

/**
* Contains dependencies.
*
Expand Down Expand Up @@ -110,9 +118,8 @@ public function __construct()
$this->session = Session::getInstance();
$this->renderer = TemplateRender::getInstance();
$this->translator = Translator::getInstance();
$this->renderer->addVars([
'ctrl' => $this,
]);
$this->logger = Logger::getInstance()->getLogger();
$this->renderer->addVars(['ctrl' => $this,]);
$this->renderer->setTemplate(strtolower($this->shortName));
$this->username = null;
$this->debugTool->stopTimer($this->shortName);
Expand Down Expand Up @@ -159,7 +166,7 @@ public function redirect(string $destiny = ''): RedirectResponse
$destiny = baseUrl('index.php');
}
$vars = ['%from%' => $this->shortName, '%to%' => $destiny];
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('redirected-from-to', $vars));
$this->logger->addDebug($this->translator->trans('redirected-from-to', $vars));
return new RedirectResponse($destiny);
}

Expand All @@ -171,7 +178,7 @@ public function redirect(string $destiny = ''): RedirectResponse
public function runMethod(string $methodName): Response
{
$method = $methodName . 'Method';
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('call-to', ['%called%' => $this->shortName . '->' . $method . '()']));
$this->logger->addDebug($this->translator->trans('call-to', ['%called%' => $this->shortName . '->' . $method . '()']));
return $this->{$method}();
}

Expand Down
13 changes: 6 additions & 7 deletions src/Alxarafe/Core/Controllers/Login.php
Expand Up @@ -9,7 +9,6 @@
use Alxarafe\Core\Base\Controller;
use Alxarafe\Core\Models\User;
use Alxarafe\Core\Providers\FlashMessages;
use Alxarafe\Core\Providers\Logger;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -135,12 +134,12 @@ public function getCookieUser($remember): ?string
$logKey = $this->request->cookies->get('logkey', '');
if ($this->username === null && $this->user->getBy('username', $user) === true) {
if ($this->user->verifyLogKey($user, $logKey)) {
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('user-logged-in-from-cookie', ['%username%' => $user]));
$this->logger->addDebug($this->translator->trans('user-logged-in-from-cookie', ['%username%' => $user]));
// Increase cookie valid time.
$time = time() + ($remember ? self::COOKIE_EXPIRATION : self::COOKIE_EXPIRATION_MIN);
$this->adjustCookieUser($time, $remember);
} else {
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('user-authentication-error'));
$this->logger->addDebug($this->translator->trans('user-authentication-error'));
$this->clearCookieUser();
}
}
Expand Down Expand Up @@ -175,7 +174,7 @@ private function adjustCookieUser(int $time = 0, int $remember = 0): void
*/
private function clearCookieUser(): void
{
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('user-cookies-cleared'));
$this->logger->addDebug($this->translator->trans('user-cookies-cleared'));
$this->username = null;
$this->user = null;
$this->logkey = null;
Expand Down Expand Up @@ -248,13 +247,13 @@ public function setUser($userName, $password, $remember = false): bool
$this->username = $this->user->username;
$time = time() + ($remember ? self::COOKIE_EXPIRATION : self::COOKIE_EXPIRATION_MIN);
$this->adjustCookieUser($time, ($remember ? self::COOKIE_EXPIRATION : self::COOKIE_EXPIRATION_MIN));
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('user-authenticated', ['%username%' => $this->user->username]));
$this->logger->addDebug($this->translator->trans('user-authenticated', ['%username%' => $this->user->username]));
} else {
$this->clearCookieUser();
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('user-authentication-wrong-password'));
$this->logger->addDebug($this->translator->trans('user-authentication-wrong-password'));
}
} else {
Logger::getInstance()->getLogger()->addDebug($this->translator->trans('user-authentication-not-found', ['%username%' => $userName]));
$this->logger->addDebug($this->translator->trans('user-authentication-not-found', ['%username%' => $userName]));
}
return $this->username !== null;
}
Expand Down

0 comments on commit 71bcf00

Please sign in to comment.