mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-22 10:11: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\user\UserInterface;
|
||||
use repository\user\User as UserRepository;
|
||||
use repository\user\UserInterface as UserRepositoryInterface;
|
||||
use entity\user\User;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -50,25 +48,33 @@ final class Core implements CoreInterface
|
||||
private $basket;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var UserRepositoryInterface
|
||||
*/
|
||||
private $userRepository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
session_start();
|
||||
$this->initSession();
|
||||
$this->initTwig();
|
||||
$this->initDatabase();
|
||||
$this->initUser();
|
||||
$this->initBasket();
|
||||
}
|
||||
|
||||
private function initSession(): void
|
||||
{
|
||||
if (!headers_sent()) {
|
||||
session_start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads basket by session
|
||||
*/
|
||||
private function initBasket(): void
|
||||
{
|
||||
if(!$_SESSION['basket']){
|
||||
if (! $_SESSION['basket']) {
|
||||
$_SESSION['basket'] = new Order();
|
||||
}
|
||||
$this->basket = $_SESSION['basket'];
|
||||
@ -79,19 +85,11 @@ final class Core implements CoreInterface
|
||||
*/
|
||||
private function initUser(): void
|
||||
{
|
||||
$this->userRepository = new UserRepository($this);
|
||||
if($_SESSION['user']){
|
||||
$this->user = $this->getUserBySession();
|
||||
if ($_SESSION['user']) {
|
||||
$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
|
||||
{
|
||||
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../template');
|
||||
@ -118,25 +116,14 @@ final class Core implements CoreInterface
|
||||
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
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->setUserSession();
|
||||
$_SESSION['user'] = $this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* {@inheritdoc}
|
||||
* @see \core\CoreInterface::getBasket()
|
||||
*/
|
||||
public function getBasket(): Order
|
||||
@ -146,7 +133,8 @@ final class Core implements CoreInterface
|
||||
|
||||
/**
|
||||
* The basket is depending on the session
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* {@inheritdoc}
|
||||
* @see \core\CoreInterface::setBasket()
|
||||
*/
|
||||
public function setBasket(Order $basket): void
|
||||
|
@ -45,7 +45,7 @@ class CoreTest extends TestCase
|
||||
}
|
||||
|
||||
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