coding-challenge-online-shop/src/core/Core.php

91 lines
1.7 KiB
PHP
Raw Normal View History

2018-07-14 16:52:17 +02:00
<?php
namespace core;
use entity\user\UserInterface;
/**
*
* @author kevinfrantz
*
2018-07-14 17:18:41 +02:00
*/
2018-07-14 16:52:17 +02:00
final class Core implements CoreInterface
{
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
const DATABASE_USERNAME = 'devuser';
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
const DATABASE_PASSWORD = 'devpass';
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
const DATABASE_NAME = 'test_db';
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
const DATABASE_PORT = '3306';
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
const DATABASE_HOST = 'codingchallengeonlineshop_db_1';
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
/**
2018-07-14 17:18:41 +02:00
*
2018-07-14 16:52:17 +02:00
* @var \Twig_Environment
*/
private $twig;
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
/**
2018-07-14 17:18:41 +02:00
*
2018-07-14 16:52:17 +02:00
* @var UserInterface
*/
private $user;
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
/**
2018-07-14 17:18:41 +02:00
*
2018-07-14 16:52:17 +02:00
* @var \PDO
*/
private $database;
2018-07-14 17:18:41 +02:00
public function __construct()
{
2018-07-14 16:52:17 +02:00
$this->initTwig();
$this->initDatabase();
2018-07-14 17:18:41 +02:00
$this->initUser();
}
/**
* Loads user by session
*/
private function initUser(): void
{
if($_SESSION['user']){
$this->user = $_SESSION['user'];
}
2018-07-14 16:52:17 +02:00
}
2018-07-14 17:18:41 +02:00
private function initTwig(): void
{
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../template');
2018-07-14 16:52:17 +02:00
$this->twig = new \Twig_Environment($loader);
}
2018-07-14 17:18:41 +02:00
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);
2018-07-14 16:52:17 +02:00
}
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
public function getDatabase(): \PDO
{
return $this->database;
}
public function getTwig(): \Twig_Environment
{
return $this->twig;
}
public function getUser(): ?UserInterface
{
return $this->user;
}
2018-07-14 17:18:41 +02:00
2018-07-14 16:52:17 +02:00
public function setUser(?UserInterface $user = null): void
{
2018-07-14 17:18:41 +02:00
$_SESSION['user'] = $this->user = $user;
2018-07-14 16:52:17 +02:00
}
}