mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-01 00:53:10 +01:00
Cleanup
This commit is contained in:
parent
990d65aa9a
commit
1492282ae5
@ -11,35 +11,41 @@ use router\Router;
|
||||
*/
|
||||
abstract class AbstractController
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CoreInterface
|
||||
*/
|
||||
protected $core;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $post;
|
||||
|
||||
public function __construct(CoreInterface $core){
|
||||
$this->core = $core;
|
||||
|
||||
public function __construct(CoreInterface $core)
|
||||
{
|
||||
$this->core = $core;
|
||||
$this->post = $_POST;
|
||||
}
|
||||
|
||||
protected function render(string $template,array $variables=[]):void{
|
||||
echo $this->core->getTwig()->render($template,$this->addUser($variables));
|
||||
|
||||
protected function render(string $template, array $variables = []): void
|
||||
{
|
||||
echo $this->core->getTwig()->render($template, $this->addUser($variables));
|
||||
}
|
||||
|
||||
private function addUser(array $variables):array{
|
||||
if(array_key_exists('user', $variables)){
|
||||
|
||||
private function addUser(array $variables): array
|
||||
{
|
||||
if (array_key_exists('user', $variables)) {
|
||||
throw new \Exception('Key user isn\'t allowed!');
|
||||
}
|
||||
$variables['user'] = $this->core->getUser();
|
||||
return $variables;
|
||||
}
|
||||
|
||||
protected function route(?array $get =[]):void{
|
||||
|
||||
protected function route(?array $get = []): void
|
||||
{
|
||||
$router = new Router();
|
||||
$router->setCore($this->core);
|
||||
$router->setGet($get);
|
||||
|
@ -22,8 +22,8 @@ class AbstractDefaultController extends AbstractController
|
||||
private function addMenuItems(array $variables): array
|
||||
{
|
||||
if (array_key_exists('menu_items', $variables)) {
|
||||
$variables['menu_items'] = array_merge($this->getMenuItems(),$variables['menu_items']);
|
||||
}else{
|
||||
$variables['menu_items'] = array_merge($this->getMenuItems(), $variables['menu_items']);
|
||||
} else {
|
||||
$variables['menu_items'] = $this->getMenuItems();
|
||||
}
|
||||
return $variables;
|
||||
@ -54,16 +54,18 @@ class AbstractDefaultController extends AbstractController
|
||||
], 'logout')
|
||||
];
|
||||
}
|
||||
return [new LinkCollection('login',[
|
||||
new Link([
|
||||
Router::CONTROLLER => 'user',
|
||||
Router::ACTION => 'login'
|
||||
], 'login'),
|
||||
new Link([
|
||||
Router::CONTROLLER => 'user',
|
||||
Router::ACTION => 'register'
|
||||
], 'register')
|
||||
])];
|
||||
return [
|
||||
new LinkCollection('login', [
|
||||
new Link([
|
||||
Router::CONTROLLER => 'user',
|
||||
Router::ACTION => 'login'
|
||||
], 'login'),
|
||||
new Link([
|
||||
Router::CONTROLLER => 'user',
|
||||
Router::ACTION => 'register'
|
||||
], 'register')
|
||||
])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,9 +45,9 @@ final class Order extends AbstractDefaultController implements OrderInterface
|
||||
private function store(): void
|
||||
{
|
||||
$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());
|
||||
}else{
|
||||
} else {
|
||||
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', [
|
||||
'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']) {
|
||||
$this->addProduct();
|
||||
}
|
||||
if ($this->post['store']){
|
||||
if ($this->post['store']) {
|
||||
$this->store();
|
||||
}
|
||||
}
|
||||
|
@ -37,9 +37,14 @@ final class Product extends AbstractDefaultController implements ProductInterfac
|
||||
}
|
||||
$this->render('product/list.html.twig', [
|
||||
'products' => $products,
|
||||
'add_to_basket'=> new Link(['controller'=>'order','action'=>'basket']),
|
||||
'add_to_basket' => new Link([
|
||||
'controller' => 'order',
|
||||
'action' => 'basket'
|
||||
]),
|
||||
'colors' => $this->getColors(),
|
||||
'menu_items'=>[$this->getColors()]
|
||||
'menu_items' => [
|
||||
$this->getColors()
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,9 @@ use controller\AbstractDefaultController;
|
||||
*/
|
||||
final class Standart extends AbstractDefaultController implements StandartInterface
|
||||
{
|
||||
public function homepage():void{
|
||||
|
||||
public function homepage(): void
|
||||
{
|
||||
$this->render('standart/homepage.html.twig');
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ namespace controller\standart;
|
||||
*/
|
||||
interface StandartInterface
|
||||
{
|
||||
public function homepage():void;
|
||||
|
||||
public function homepage(): void;
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ use entity\user\User as UserEntity;
|
||||
*/
|
||||
final class User extends AbstractDefaultController implements UserInterface
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var UserRepository
|
||||
@ -68,7 +69,7 @@ final class User extends AbstractDefaultController implements UserInterface
|
||||
$this->route();
|
||||
}
|
||||
|
||||
private function validateRegistrationData():bool
|
||||
private function validateRegistrationData(): bool
|
||||
{
|
||||
if (! filter_var($this->post['email'], FILTER_VALIDATE_EMAIL)) {
|
||||
throw new \Exception('Not a valid email!');
|
||||
|
@ -64,7 +64,7 @@ final class Core implements CoreInterface
|
||||
|
||||
private function initSession(): void
|
||||
{
|
||||
if (!headers_sent()) {
|
||||
if (! headers_sent()) {
|
||||
session_start();
|
||||
}
|
||||
}
|
||||
|
@ -7,19 +7,20 @@ use entity\order\Order;
|
||||
/**
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
*
|
||||
*/
|
||||
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 getBasket():Order;
|
||||
|
||||
public function setBasket(Order $basket):void;
|
||||
public function getUser(): ?UserInterface;
|
||||
|
||||
public function setUser(?UserInterface $user = null): void;
|
||||
|
||||
public function getBasket(): Order;
|
||||
|
||||
public function setBasket(Order $basket): void;
|
||||
}
|
||||
|
@ -11,19 +11,21 @@ use entity\user\User;
|
||||
*/
|
||||
class CoreTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @var Core
|
||||
*/
|
||||
protected $core;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
protected function setUp():void{
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->core = new Core();
|
||||
$this->user = new User();
|
||||
$this->user->setId(1);
|
||||
@ -31,21 +33,26 @@ class CoreTest extends TestCase
|
||||
$this->user->setPasswordHashByPassword('passwort:)');
|
||||
$this->core->setUser($this->user);
|
||||
}
|
||||
|
||||
public function testTwig():void{
|
||||
|
||||
public function testTwig(): void
|
||||
{
|
||||
$this->assertInstanceOf(\Twig_Environment::class, $this->core->getTwig());
|
||||
}
|
||||
|
||||
public function testDatabase():void{
|
||||
|
||||
public function testDatabase(): void
|
||||
{
|
||||
$this->assertInstanceOf(\PDO::class, $this->core->getDatabase());
|
||||
}
|
||||
|
||||
public function testUser():void{
|
||||
|
||||
public function testUser(): void
|
||||
{
|
||||
$this->assertEquals($this->user, $this->core->getUser());
|
||||
}
|
||||
|
||||
public function testSession():void{
|
||||
$this->assertEquals($this->core->getUser()->getPasswordHash(), $_SESSION['user']->getPasswordHash());
|
||||
|
||||
public function testSession(): void
|
||||
{
|
||||
$this->assertEquals($this->core->getUser()
|
||||
->getPasswordHash(), $_SESSION['user']->getPasswordHash());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
<?php
|
||||
namespace entity\payment\method1;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
*/
|
||||
class Method1Test
|
||||
class Method1Test extends TestCase
|
||||
{
|
||||
public function testName():void{
|
||||
$this->assertEquals(Method1::getName(), 'method1');
|
||||
|
@ -22,12 +22,13 @@
|
||||
</a>
|
||||
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
|
||||
{%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 %}
|
||||
</div></li>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
|
@ -28,8 +28,7 @@ basket
|
||||
{% for key,payment in payment_methods %}
|
||||
<option value="{{ key }}">{{ payment.name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<input type="hidden" name="store" value="1" />
|
||||
<input type="submit" class="btn btn-primary" value="Order" />
|
||||
</select> <input type="hidden" name="store" value="1" /> <input type="submit"
|
||||
class="btn btn-primary" value="Order" />
|
||||
</form>
|
||||
{% endblock %}
|
@ -23,8 +23,8 @@ product overview
|
||||
</ul>
|
||||
</span>
|
||||
<form action="{{ add_to_basket.url }}" method="post">
|
||||
<input type="hidden" name="add" value="{{ product.id }}" />
|
||||
<input type="submit" class="btn btn-primary" value="Add to basket" />
|
||||
<input type="hidden" name="add" value="{{ product.id }}" /> <input
|
||||
type="submit" class="btn btn-primary" value="Add to basket" />
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -1,5 +1,7 @@
|
||||
{% extends "frames/default.html.twig" %}
|
||||
{% block title %}Homepage{% endblock %}
|
||||
{% block title %}
|
||||
Homepage
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
<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" />
|
||||
|
@ -1,16 +1,20 @@
|
||||
{% extends "frames/default.html.twig" %}
|
||||
{% block title %}login{% endblock %}
|
||||
{% block title %}
|
||||
login
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
<h1>Login</h1>
|
||||
<form method='post'>
|
||||
<div class="form-group">
|
||||
<label for="email">Email address</label>
|
||||
<input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" name="password" class="form-control" id="password" placeholder="Password">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
<div class="form-group">
|
||||
<label for="email">Email address</label> <input type="email"
|
||||
name="email" class="form-control" id="email"
|
||||
aria-describedby="emailHelp" placeholder="Enter email">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label> <input type="password"
|
||||
name="password" class="form-control" id="password"
|
||||
placeholder="Password">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
@ -1,20 +1,25 @@
|
||||
{% extends "frames/default.html.twig" %}
|
||||
{% block title %}register{% endblock %}
|
||||
{% block title %}
|
||||
register
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
<h1>Register</h1>
|
||||
<form method="post">
|
||||
<div class="form-group">
|
||||
<label for="email">Email address</label>
|
||||
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" name="email" placeholder="Enter email">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="name">Username</label>
|
||||
<input type="email" class="form-control" id="name" aria-describedby="emailHelp" name="name" placeholder="Enter Username">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
<div class="form-group">
|
||||
<label for="email">Email address</label> <input type="email"
|
||||
class="form-control" id="email" aria-describedby="emailHelp"
|
||||
name="email" placeholder="Enter email">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="name">Username</label> <input type="email"
|
||||
class="form-control" id="name" aria-describedby="emailHelp"
|
||||
name="name" placeholder="Enter Username">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label> <input type="password"
|
||||
class="form-control" id="password" name="password"
|
||||
placeholder="Password">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
Loading…
Reference in New Issue
Block a user