mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-22 18:21:04 +01:00
Optimized Sessions
This commit is contained in:
parent
228c261db1
commit
18382d6633
@ -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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -50,19 +48,27 @@ final class Core implements CoreInterface
|
|||||||
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
|
||||||
*/
|
*/
|
||||||
@ -79,19 +85,11 @@ final class Core implements CoreInterface
|
|||||||
*/
|
*/
|
||||||
private function initUser(): void
|
private function initUser(): void
|
||||||
{
|
{
|
||||||
$this->userRepository = new UserRepository($this);
|
|
||||||
if ($_SESSION['user']) {
|
if ($_SESSION['user']) {
|
||||||
$this->user = $this->getUserBySession();
|
$this->user = $_SESSION['user'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../template');
|
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../template');
|
||||||
@ -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
|
||||||
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user