From 67f6ea76eb3e35c2683105f94fe47d48e299d2d4 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sat, 14 Jul 2018 17:18:41 +0200 Subject: [PATCH] Implemented session --- src/core/Core.php | 57 ++++++++++++++++++++++++++++--------------- src/core/CoreTest.php | 4 +++ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/core/Core.php b/src/core/Core.php index b83e952..6d8eb0a 100644 --- a/src/core/Core.php +++ b/src/core/Core.php @@ -7,48 +7,66 @@ use entity\user\UserInterface; * * @author kevinfrantz * - */ + */ final class Core implements CoreInterface { + const DATABASE_USERNAME = 'devuser'; - + const DATABASE_PASSWORD = 'devpass'; - + const DATABASE_NAME = 'test_db'; - + const DATABASE_PORT = '3306'; - + const DATABASE_HOST = 'codingchallengeonlineshop_db_1'; - + /** + * * @var \Twig_Environment */ private $twig; - + /** + * * @var UserInterface */ private $user; - + /** + * * @var \PDO */ private $database; - - public function __construct(){ + + public function __construct() + { $this->initTwig(); $this->initDatabase(); + $this->initUser(); } - - private function initTwig():void{ - $loader = new \Twig_Loader_Filesystem(__DIR__.'/../template'); + + /** + * Loads user by session + */ + private function initUser(): void + { + if($_SESSION['user']){ + $this->user = $_SESSION['user']; + } + } + + private function initTwig(): void + { + $loader = new \Twig_Loader_Filesystem(__DIR__ . '/../template'); $this->twig = new \Twig_Environment($loader); } - - private function initDatabase():void{ - $this->database = new \PDO('mysql:host='.self::DATABASE_HOST.';dbname='.self::DATABASE_NAME.';port='.self::DATABASE_PORT, self::DATABASE_USERNAME, self::DATABASE_PASSWORD); + + private function initDatabase(): void + { + $this->database = new \PDO('mysql:host=' . self::DATABASE_HOST . ';dbname=' . self::DATABASE_NAME . ';port=' . self::DATABASE_PORT, self::DATABASE_USERNAME, self::DATABASE_PASSWORD); } - + public function getDatabase(): \PDO { return $this->database; @@ -63,11 +81,10 @@ final class Core implements CoreInterface { return $this->user; } - + public function setUser(?UserInterface $user = null): void { - $this->user = $user; + $_SESSION['user'] = $this->user = $user; } - } diff --git a/src/core/CoreTest.php b/src/core/CoreTest.php index b9fc4f0..edf8e32 100644 --- a/src/core/CoreTest.php +++ b/src/core/CoreTest.php @@ -40,5 +40,9 @@ class CoreTest extends TestCase public function testUser():void{ $this->assertEquals($this->user, $this->core->getUser()); } + + public function testSession():void{ + $this->assertEquals($this->core->getUser(), $_SESSION['user']); + } }