mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-26 03:51:06 +01:00
Implemented session
This commit is contained in:
parent
b11cea5337
commit
67f6ea76eb
@ -10,6 +10,7 @@ use entity\user\UserInterface;
|
|||||||
*/
|
*/
|
||||||
final class Core implements CoreInterface
|
final class Core implements CoreInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
const DATABASE_USERNAME = 'devuser';
|
const DATABASE_USERNAME = 'devuser';
|
||||||
|
|
||||||
const DATABASE_PASSWORD = 'devpass';
|
const DATABASE_PASSWORD = 'devpass';
|
||||||
@ -21,32 +22,49 @@ final class Core implements CoreInterface
|
|||||||
const DATABASE_HOST = 'codingchallengeonlineshop_db_1';
|
const DATABASE_HOST = 'codingchallengeonlineshop_db_1';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @var \Twig_Environment
|
* @var \Twig_Environment
|
||||||
*/
|
*/
|
||||||
private $twig;
|
private $twig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @var UserInterface
|
* @var UserInterface
|
||||||
*/
|
*/
|
||||||
private $user;
|
private $user;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @var \PDO
|
* @var \PDO
|
||||||
*/
|
*/
|
||||||
private $database;
|
private $database;
|
||||||
|
|
||||||
public function __construct(){
|
public function __construct()
|
||||||
|
{
|
||||||
$this->initTwig();
|
$this->initTwig();
|
||||||
$this->initDatabase();
|
$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);
|
$this->twig = new \Twig_Environment($loader);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function initDatabase():void{
|
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);
|
{
|
||||||
|
$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
|
public function getDatabase(): \PDO
|
||||||
@ -66,8 +84,7 @@ final class Core implements CoreInterface
|
|||||||
|
|
||||||
public function setUser(?UserInterface $user = null): void
|
public function setUser(?UserInterface $user = null): void
|
||||||
{
|
{
|
||||||
$this->user = $user;
|
$_SESSION['user'] = $this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,5 +40,9 @@ class CoreTest extends TestCase
|
|||||||
public function testUser():void{
|
public function testUser():void{
|
||||||
$this->assertEquals($this->user, $this->core->getUser());
|
$this->assertEquals($this->user, $this->core->getUser());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSession():void{
|
||||||
|
$this->assertEquals($this->core->getUser(), $_SESSION['user']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user