Optimized Sessions

This commit is contained in:
Kevin Frantz 2018-07-15 13:57:49 +02:00
parent 228c261db1
commit 18382d6633
2 changed files with 23 additions and 35 deletions

View File

@ -3,9 +3,7 @@ namespace core;
use entity\order\Order; use entity\order\Order;
use entity\user\UserInterface; use entity\user\UserInterface;
use repository\user\User as UserRepository;
use repository\user\UserInterface as UserRepositoryInterface; use repository\user\UserInterface as UserRepositoryInterface;
use entity\user\User;
/** /**
* *
@ -44,53 +42,53 @@ final class Core implements CoreInterface
private $database; private $database;
/** /**
* *
* @var Order * @var Order
*/ */
private $basket; private $basket;
/** /**
*
* @var UserRepositoryInterface * @var UserRepositoryInterface
*/ */
private $userRepository; private $userRepository;
public function __construct() public function __construct()
{ {
session_start(); $this->initSession();
$this->initTwig(); $this->initTwig();
$this->initDatabase(); $this->initDatabase();
$this->initUser(); $this->initUser();
$this->initBasket(); $this->initBasket();
} }
private function initSession(): void
{
if (!headers_sent()) {
session_start();
}
}
/** /**
* Loads basket by session * Loads basket by session
*/ */
private function initBasket(): void private function initBasket(): void
{ {
if(!$_SESSION['basket']){ if (! $_SESSION['basket']) {
$_SESSION['basket'] = new Order(); $_SESSION['basket'] = new Order();
} }
$this->basket = $_SESSION['basket']; $this->basket = $_SESSION['basket'];
} }
/** /**
* Loads user by session * Loads user by session
*/ */
private function initUser(): void private function initUser(): void
{ {
$this->userRepository = new UserRepository($this); if ($_SESSION['user']) {
if($_SESSION['user']){ $this->user = $_SESSION['user'];
$this->user = $this->getUserBySession();
} }
} }
private function getUserBySession():UserInterface{
$user = new User();
$user->setPasswordHash($_SESSION['user']['hash']);
$user->setEmail($_SESSION['user']['email']);
return $this->userRepository->getUserByMailAndHash($user);
}
private function initTwig(): void private function initTwig(): void
{ {
@ -118,25 +116,14 @@ final class Core implements CoreInterface
return $this->user; return $this->user;
} }
private function setUserSession():void{
if($this->user){
$_SESSION['user'] = [
'email'=>$this->user->getEmail(),
'hash'=>$this->user->getPasswordHash(),
];
}else{
unset($_SESSION['user']);
}
}
public function setUser(?UserInterface $user = null): void public function setUser(?UserInterface $user = null): void
{ {
$this->user = $user; $_SESSION['user'] = $this->user = $user;
$this->setUserSession();
} }
/** /**
* {@inheritDoc} *
* {@inheritdoc}
* @see \core\CoreInterface::getBasket() * @see \core\CoreInterface::getBasket()
*/ */
public function getBasket(): Order public function getBasket(): Order
@ -146,7 +133,8 @@ final class Core implements CoreInterface
/** /**
* The basket is depending on the session * The basket is depending on the session
* {@inheritDoc} *
* {@inheritdoc}
* @see \core\CoreInterface::setBasket() * @see \core\CoreInterface::setBasket()
*/ */
public function setBasket(Order $basket): void public function setBasket(Order $basket): void

View File

@ -45,7 +45,7 @@ class CoreTest extends TestCase
} }
public function testSession():void{ public function testSession():void{
$this->assertEquals($this->core->getUser()->getPasswordHash(), $_SESSION['user']['hash']); $this->assertEquals($this->core->getUser()->getPasswordHash(), $_SESSION['user']->getPasswordHash());
} }
} }