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
|
abstract class AbstractController
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @var CoreInterface
|
* @var CoreInterface
|
||||||
*/
|
*/
|
||||||
protected $core;
|
protected $core;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
|
@ -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,16 +54,18 @@ class AbstractDefaultController extends AbstractController
|
|||||||
], 'logout')
|
], 'logout')
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
return [new LinkCollection('login',[
|
return [
|
||||||
new Link([
|
new LinkCollection('login', [
|
||||||
Router::CONTROLLER => 'user',
|
new Link([
|
||||||
Router::ACTION => 'login'
|
Router::CONTROLLER => 'user',
|
||||||
], 'login'),
|
Router::ACTION => 'login'
|
||||||
new Link([
|
], 'login'),
|
||||||
Router::CONTROLLER => 'user',
|
new Link([
|
||||||
Router::ACTION => 'register'
|
Router::CONTROLLER => 'user',
|
||||||
], 'register')
|
Router::ACTION => 'register'
|
||||||
])];
|
], 'register')
|
||||||
|
])
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
|
]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ namespace controller\standart;
|
|||||||
*/
|
*/
|
||||||
interface StandartInterface
|
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
|
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!');
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,19 +7,20 @@ use entity\order\Order;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -11,19 +11,21 @@ use entity\user\User;
|
|||||||
*/
|
*/
|
||||||
class CoreTest extends TestCase
|
class CoreTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @var Core
|
* @var Core
|
||||||
*/
|
*/
|
||||||
protected $core;
|
protected $core;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @var User
|
* @var User
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
@ -31,21 +33,26 @@ class CoreTest extends TestCase
|
|||||||
$this->user->setPasswordHashByPassword('passwort:)');
|
$this->user->setPasswordHashByPassword('passwort:)');
|
||||||
$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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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');
|
||||||
|
@ -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>
|
||||||
|
@ -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 %}
|
@ -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>
|
||||||
|
@ -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" />
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
{% 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"
|
||||||
</div>
|
aria-describedby="emailHelp" placeholder="Enter email">
|
||||||
<div class="form-group">
|
</div>
|
||||||
<label for="password">Password</label>
|
<div class="form-group">
|
||||||
<input type="password" name="password" class="form-control" id="password" placeholder="Password">
|
<label for="password">Password</label> <input type="password"
|
||||||
</div>
|
name="password" class="form-control" id="password"
|
||||||
<button type="submit" class="btn btn-primary">Submit</button>
|
placeholder="Password">
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -1,20 +1,25 @@
|
|||||||
{% 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"
|
||||||
</div>
|
name="email" placeholder="Enter email">
|
||||||
<div class="form-group">
|
</div>
|
||||||
<label for="name">Username</label>
|
<div class="form-group">
|
||||||
<input type="email" class="form-control" id="name" aria-describedby="emailHelp" name="name" placeholder="Enter Username">
|
<label for="name">Username</label> <input type="email"
|
||||||
</div>
|
class="form-control" id="name" aria-describedby="emailHelp"
|
||||||
<div class="form-group">
|
name="name" placeholder="Enter Username">
|
||||||
<label for="password">Password</label>
|
</div>
|
||||||
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
|
<div class="form-group">
|
||||||
</div>
|
<label for="password">Password</label> <input type="password"
|
||||||
<button type="submit" class="btn btn-primary">Submit</button>
|
class="form-control" id="password" name="password"
|
||||||
|
placeholder="Password">
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Loading…
Reference in New Issue
Block a user