From 18382d663380be34c72e7859512ef3f32d80d975 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sun, 15 Jul 2018 13:57:49 +0200 Subject: [PATCH] Optimized Sessions --- src/core/Core.php | 56 +++++++++++++++++-------------------------- src/core/CoreTest.php | 2 +- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/core/Core.php b/src/core/Core.php index 369b573..16dfa21 100644 --- a/src/core/Core.php +++ b/src/core/Core.php @@ -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; /** * @@ -44,53 +42,53 @@ final class Core implements CoreInterface private $database; /** - * + * * @var Order */ 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']; } - + /** * Loads user by session */ 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 { @@ -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 diff --git a/src/core/CoreTest.php b/src/core/CoreTest.php index 455c40e..de0275d 100644 --- a/src/core/CoreTest.php +++ b/src/core/CoreTest.php @@ -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()); } }