This commit is contained in:
Kevin Frantz 2018-07-15 16:41:56 +02:00
parent 990d65aa9a
commit 1492282ae5
17 changed files with 129 additions and 91 deletions

View File

@ -11,7 +11,9 @@ use router\Router;
*/ */
abstract class AbstractController abstract class AbstractController
{ {
/** /**
*
* @var CoreInterface * @var CoreInterface
*/ */
protected $core; protected $core;
@ -22,24 +24,28 @@ abstract class AbstractController
*/ */
protected $post; protected $post;
public function __construct(CoreInterface $core){ public function __construct(CoreInterface $core)
{
$this->core = $core; $this->core = $core;
$this->post = $_POST; $this->post = $_POST;
} }
protected function render(string $template,array $variables=[]):void{ protected function render(string $template, array $variables = []): void
echo $this->core->getTwig()->render($template,$this->addUser($variables)); {
echo $this->core->getTwig()->render($template, $this->addUser($variables));
} }
private function addUser(array $variables):array{ private function addUser(array $variables): array
if(array_key_exists('user', $variables)){ {
if (array_key_exists('user', $variables)) {
throw new \Exception('Key user isn\'t allowed!'); throw new \Exception('Key user isn\'t allowed!');
} }
$variables['user'] = $this->core->getUser(); $variables['user'] = $this->core->getUser();
return $variables; return $variables;
} }
protected function route(?array $get =[]):void{ protected function route(?array $get = []): void
{
$router = new Router(); $router = new Router();
$router->setCore($this->core); $router->setCore($this->core);
$router->setGet($get); $router->setGet($get);

View File

@ -22,8 +22,8 @@ class AbstractDefaultController extends AbstractController
private function addMenuItems(array $variables): array private function addMenuItems(array $variables): array
{ {
if (array_key_exists('menu_items', $variables)) { if (array_key_exists('menu_items', $variables)) {
$variables['menu_items'] = array_merge($this->getMenuItems(),$variables['menu_items']); $variables['menu_items'] = array_merge($this->getMenuItems(), $variables['menu_items']);
}else{ } else {
$variables['menu_items'] = $this->getMenuItems(); $variables['menu_items'] = $this->getMenuItems();
} }
return $variables; return $variables;
@ -54,7 +54,8 @@ class AbstractDefaultController extends AbstractController
], 'logout') ], 'logout')
]; ];
} }
return [new LinkCollection('login',[ return [
new LinkCollection('login', [
new Link([ new Link([
Router::CONTROLLER => 'user', Router::CONTROLLER => 'user',
Router::ACTION => 'login' Router::ACTION => 'login'
@ -63,7 +64,8 @@ class AbstractDefaultController extends AbstractController
Router::CONTROLLER => 'user', Router::CONTROLLER => 'user',
Router::ACTION => 'register' Router::ACTION => 'register'
], 'register') ], 'register')
])]; ])
];
} }
} }

View File

@ -45,9 +45,9 @@ final class Order extends AbstractDefaultController implements OrderInterface
private function store(): void private function store(): void
{ {
$this->core->getBasket()->setCustomer($this->core->getUser()); $this->core->getBasket()->setCustomer($this->core->getUser());
if($this->orderRepository->saveOrder($this->core->getBasket())){ if ($this->orderRepository->saveOrder($this->core->getBasket())) {
$this->core->setBasket(new OrderEntity()); $this->core->setBasket(new OrderEntity());
}else{ } else {
throw new \Exception('Order could not be saved!'); throw new \Exception('Order could not be saved!');
} }
} }
@ -59,7 +59,7 @@ final class Order extends AbstractDefaultController implements OrderInterface
} }
$this->render('order/basket.html.twig', [ $this->render('order/basket.html.twig', [
'basket' => $this->core->getBasket(), 'basket' => $this->core->getBasket(),
'payment_methods'=>AbstractPayment::getPaymentMethods(), 'payment_methods' => AbstractPayment::getPaymentMethods()
]); ]);
} }
@ -68,7 +68,7 @@ final class Order extends AbstractDefaultController implements OrderInterface
if ($this->post['add']) { if ($this->post['add']) {
$this->addProduct(); $this->addProduct();
} }
if ($this->post['store']){ if ($this->post['store']) {
$this->store(); $this->store();
} }
} }

View File

@ -37,9 +37,14 @@ final class Product extends AbstractDefaultController implements ProductInterfac
} }
$this->render('product/list.html.twig', [ $this->render('product/list.html.twig', [
'products' => $products, 'products' => $products,
'add_to_basket'=> new Link(['controller'=>'order','action'=>'basket']), 'add_to_basket' => new Link([
'controller' => 'order',
'action' => 'basket'
]),
'colors' => $this->getColors(), 'colors' => $this->getColors(),
'menu_items'=>[$this->getColors()] 'menu_items' => [
$this->getColors()
]
]); ]);
} }

View File

@ -10,7 +10,9 @@ use controller\AbstractDefaultController;
*/ */
final class Standart extends AbstractDefaultController implements StandartInterface final class Standart extends AbstractDefaultController implements StandartInterface
{ {
public function homepage():void{
public function homepage(): void
{
$this->render('standart/homepage.html.twig'); $this->render('standart/homepage.html.twig');
} }
} }

View File

@ -8,6 +8,7 @@ namespace controller\standart;
*/ */
interface StandartInterface interface StandartInterface
{ {
public function homepage():void;
public function homepage(): void;
} }

View File

@ -13,6 +13,7 @@ use entity\user\User as UserEntity;
*/ */
final class User extends AbstractDefaultController implements UserInterface final class User extends AbstractDefaultController implements UserInterface
{ {
/** /**
* *
* @var UserRepository * @var UserRepository
@ -68,7 +69,7 @@ final class User extends AbstractDefaultController implements UserInterface
$this->route(); $this->route();
} }
private function validateRegistrationData():bool private function validateRegistrationData(): bool
{ {
if (! filter_var($this->post['email'], FILTER_VALIDATE_EMAIL)) { if (! filter_var($this->post['email'], FILTER_VALIDATE_EMAIL)) {
throw new \Exception('Not a valid email!'); throw new \Exception('Not a valid email!');

View File

@ -64,7 +64,7 @@ final class Core implements CoreInterface
private function initSession(): void private function initSession(): void
{ {
if (!headers_sent()) { if (! headers_sent()) {
session_start(); session_start();
} }
} }

View File

@ -11,15 +11,16 @@ use entity\order\Order;
*/ */
interface CoreInterface interface CoreInterface
{ {
public function getDatabase():\PDO;
public function getTwig():\Twig_Environment; public function getDatabase(): \PDO;
public function getUser():?UserInterface; public function getTwig(): \Twig_Environment;
public function setUser(?UserInterface $user = null):void; public function getUser(): ?UserInterface;
public function getBasket():Order; public function setUser(?UserInterface $user = null): void;
public function setBasket(Order $basket):void; public function getBasket(): Order;
public function setBasket(Order $basket): void;
} }

View File

@ -11,6 +11,7 @@ use entity\user\User;
*/ */
class CoreTest extends TestCase class CoreTest extends TestCase
{ {
/** /**
* *
* @var Core * @var Core
@ -23,7 +24,8 @@ class CoreTest extends TestCase
*/ */
protected $user; protected $user;
protected function setUp():void{ protected function setUp(): void
{
$this->core = new Core(); $this->core = new Core();
$this->user = new User(); $this->user = new User();
$this->user->setId(1); $this->user->setId(1);
@ -32,20 +34,25 @@ class CoreTest extends TestCase
$this->core->setUser($this->user); $this->core->setUser($this->user);
} }
public function testTwig():void{ public function testTwig(): void
{
$this->assertInstanceOf(\Twig_Environment::class, $this->core->getTwig()); $this->assertInstanceOf(\Twig_Environment::class, $this->core->getTwig());
} }
public function testDatabase():void{ public function testDatabase(): void
{
$this->assertInstanceOf(\PDO::class, $this->core->getDatabase()); $this->assertInstanceOf(\PDO::class, $this->core->getDatabase());
} }
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{ public function testSession(): void
$this->assertEquals($this->core->getUser()->getPasswordHash(), $_SESSION['user']->getPasswordHash()); {
$this->assertEquals($this->core->getUser()
->getPasswordHash(), $_SESSION['user']->getPasswordHash());
} }
} }

View File

@ -1,12 +1,14 @@
<?php <?php
namespace entity\payment\method1; namespace entity\payment\method1;
use PHPUnit\Framework\TestCase;
/** /**
* *
* @author kevinfrantz * @author kevinfrantz
* *
*/ */
class Method1Test class Method1Test extends TestCase
{ {
public function testName():void{ public function testName():void{
$this->assertEquals(Method1::getName(), 'method1'); $this->assertEquals(Method1::getName(), 'method1');

View File

@ -22,12 +22,13 @@
</a> </a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown"> <div class="dropdown-menu" aria-labelledby="navbarDropdown">
{%for link in item %} {%for link in item %}
<a class="dropdown-item {% if link.active %}active{% endif %}" href="{{ link.url }}">{{ link.name }}</a> <a class="dropdown-item {% if link.active %}active{% endif %}"
href="{{ link.url }}"> {{ link.name }}
</a>
{% endfor %} {% endfor %}
</div></li> </div></li>
{% endif %} {% endif %}
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

View File

@ -28,8 +28,7 @@ basket
{% for key,payment in payment_methods %} {% for key,payment in payment_methods %}
<option value="{{ key }}">{{ payment.name }}</option> <option value="{{ key }}">{{ payment.name }}</option>
{% endfor %} {% endfor %}
</select> </select> <input type="hidden" name="store" value="1" /> <input type="submit"
<input type="hidden" name="store" value="1" /> class="btn btn-primary" value="Order" />
<input type="submit" class="btn btn-primary" value="Order" />
</form> </form>
{% endblock %} {% endblock %}

View File

@ -23,8 +23,8 @@ product overview
</ul> </ul>
</span> </span>
<form action="{{ add_to_basket.url }}" method="post"> <form action="{{ add_to_basket.url }}" method="post">
<input type="hidden" name="add" value="{{ product.id }}" /> <input type="hidden" name="add" value="{{ product.id }}" /> <input
<input type="submit" class="btn btn-primary" value="Add to basket" /> type="submit" class="btn btn-primary" value="Add to basket" />
</form> </form>
</div> </div>
</div> </div>

View File

@ -1,5 +1,7 @@
{% extends "frames/default.html.twig" %} {% extends "frames/default.html.twig" %}
{% block title %}Homepage{% endblock %} {% block title %}
Homepage
{% endblock %}
{% block content %} {% block content %}
<h2>Welcome to the online shop!</h2> <h2>Welcome to the online shop!</h2>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Signs%2C_signs_and_more_signs_%282504183343%29.jpg/1280px-Signs%2C_signs_and_more_signs_%282504183343%29.jpg" /> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Signs%2C_signs_and_more_signs_%282504183343%29.jpg/1280px-Signs%2C_signs_and_more_signs_%282504183343%29.jpg" />

View File

@ -1,15 +1,19 @@
{% extends "frames/default.html.twig" %} {% extends "frames/default.html.twig" %}
{% block title %}login{% endblock %} {% block title %}
login
{% endblock %}
{% block content %} {% block content %}
<h1>Login</h1> <h1>Login</h1>
<form method='post'> <form method='post'>
<div class="form-group"> <div class="form-group">
<label for="email">Email address</label> <label for="email">Email address</label> <input type="email"
<input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email"> name="email" class="form-control" id="email"
aria-describedby="emailHelp" placeholder="Enter email">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="password">Password</label> <label for="password">Password</label> <input type="password"
<input type="password" name="password" class="form-control" id="password" placeholder="Password"> name="password" class="form-control" id="password"
placeholder="Password">
</div> </div>
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>
</form> </form>

View File

@ -1,19 +1,24 @@
{% extends "frames/default.html.twig" %} {% extends "frames/default.html.twig" %}
{% block title %}register{% endblock %} {% block title %}
register
{% endblock %}
{% block content %} {% block content %}
<h1>Register</h1> <h1>Register</h1>
<form method="post"> <form method="post">
<div class="form-group"> <div class="form-group">
<label for="email">Email address</label> <label for="email">Email address</label> <input type="email"
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" name="email" placeholder="Enter email"> class="form-control" id="email" aria-describedby="emailHelp"
name="email" placeholder="Enter email">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="name">Username</label> <label for="name">Username</label> <input type="email"
<input type="email" class="form-control" id="name" aria-describedby="emailHelp" name="name" placeholder="Enter Username"> class="form-control" id="name" aria-describedby="emailHelp"
name="name" placeholder="Enter Username">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="password">Password</label> <label for="password">Password</label> <input type="password"
<input type="password" class="form-control" id="password" name="password" placeholder="Password"> class="form-control" id="password" name="password"
placeholder="Password">
</div> </div>
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>
</form> </form>