mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-12-22 23:25:03 +01:00
Implemented exception
This commit is contained in:
parent
a444f4805c
commit
2ec0a66896
@ -2,8 +2,6 @@
|
||||
namespace controller\user;
|
||||
|
||||
use controller\AbstractDefaultController;
|
||||
use router\Router;
|
||||
use controller\AbstractController;
|
||||
use core\CoreInterface;
|
||||
use repository\user\User as UserRepository;
|
||||
use entity\user\User as UserEntity;
|
||||
@ -15,7 +13,6 @@ use entity\user\User as UserEntity;
|
||||
*/
|
||||
final class User extends AbstractDefaultController implements UserInterface
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var UserRepository
|
||||
@ -37,11 +34,7 @@ final class User extends AbstractDefaultController implements UserInterface
|
||||
public function login(): void
|
||||
{
|
||||
if ($this->post) {
|
||||
try {
|
||||
$this->loginRoutine();
|
||||
} catch (\Exception $exception) {
|
||||
$this->render('frames/exception.html.twig',['message'=>$exception->getMessage()]);
|
||||
}
|
||||
$this->loginRoutine();
|
||||
} else {
|
||||
$this->render('user/login.html.twig');
|
||||
}
|
||||
@ -58,7 +51,35 @@ final class User extends AbstractDefaultController implements UserInterface
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
$this->render('user/register.html.twig');
|
||||
if ($this->post && $this->validateRegistrationData()) {
|
||||
$this->registerRoutine();
|
||||
} else {
|
||||
$this->render('user/register.html.twig');
|
||||
}
|
||||
}
|
||||
|
||||
private function registerRoutine(): void
|
||||
{
|
||||
$requestedUser = new UserEntity();
|
||||
$requestedUser->setPasswordHashByPassword($this->post['password']);
|
||||
$requestedUser->setName($this->post['name']);
|
||||
$requestedUser->setEmail($this->post['email']);
|
||||
$this->repository->addUser($requestedUser);
|
||||
$this->route();
|
||||
}
|
||||
|
||||
private function validateRegistrationData():bool
|
||||
{
|
||||
if (! filter_var($this->post['email'], FILTER_VALIDATE_EMAIL)) {
|
||||
throw new \Exception('Not a valid email!');
|
||||
}
|
||||
if (strlen($this->post['name']) < 1) {
|
||||
throw new \Exception('Name to short!');
|
||||
}
|
||||
if (strlen($this->post['password']) < 8) {
|
||||
throw new \Exception('Password to short!');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,10 +14,11 @@ use controller\order\Order;
|
||||
*/
|
||||
final class Router implements RouterInterface
|
||||
{
|
||||
const CONTROLLER='controller';
|
||||
|
||||
|
||||
const CONTROLLER = 'controller';
|
||||
|
||||
const ACTION = 'action';
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CoreInterface
|
||||
@ -40,42 +41,56 @@ final class Router implements RouterInterface
|
||||
*/
|
||||
public function route()
|
||||
{
|
||||
if ($this->get) {
|
||||
switch ($this->get[self::CONTROLLER]) {
|
||||
case 'user':
|
||||
$userController = new User($this->core);
|
||||
switch ($this->get[self::ACTION]) {
|
||||
case 'login':
|
||||
return $userController->login();
|
||||
case 'logout':
|
||||
return $userController->logout();
|
||||
case 'register':
|
||||
return $userController->register();
|
||||
}
|
||||
case 'product':
|
||||
$productController = new Product($this->core);
|
||||
switch ($this->get[self::ACTION]) {
|
||||
case 'list':
|
||||
return $productController->list(($this->get['color'])?$this->get['color']:null);
|
||||
}
|
||||
case 'order':
|
||||
$orderController = new Order($this->core);
|
||||
switch ($this->get[self::ACTION]){
|
||||
case 'store':
|
||||
return $orderController->store();
|
||||
case 'basket':
|
||||
return $orderController->basket();
|
||||
case 'payment':
|
||||
return $orderController->selectPaymentMethod();
|
||||
case 'add-product':
|
||||
return $orderController->addProduct();
|
||||
}
|
||||
/**
|
||||
* From a security perspective this try catch is not a good idea
|
||||
* It's just here for usability reasons and to save code ;)
|
||||
*/
|
||||
try {
|
||||
if ($this->get) {
|
||||
switch ($this->get[self::CONTROLLER]) {
|
||||
case 'user':
|
||||
$userController = new User($this->core);
|
||||
switch ($this->get[self::ACTION]) {
|
||||
case 'login':
|
||||
return $userController->login();
|
||||
case 'logout':
|
||||
return $userController->logout();
|
||||
case 'register':
|
||||
return $userController->register();
|
||||
}
|
||||
case 'product':
|
||||
$productController = new Product($this->core);
|
||||
switch ($this->get[self::ACTION]) {
|
||||
case 'list':
|
||||
return $productController->list(($this->get['color']) ? $this->get['color'] : null);
|
||||
}
|
||||
case 'order':
|
||||
$orderController = new Order($this->core);
|
||||
switch ($this->get[self::ACTION]) {
|
||||
case 'store':
|
||||
return $orderController->store();
|
||||
case 'basket':
|
||||
return $orderController->basket();
|
||||
case 'payment':
|
||||
return $orderController->selectPaymentMethod();
|
||||
case 'add-product':
|
||||
return $orderController->addProduct();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$standartController = new Standart($this->core);
|
||||
return $standartController->homepage();
|
||||
}
|
||||
} else {
|
||||
$standartController = new Standart($this->core);
|
||||
return $standartController->homepage();
|
||||
throw new \Exception('Route not found!');
|
||||
} catch (\Exception $exception) {
|
||||
$this->echoException($exception);
|
||||
}
|
||||
throw new \Exception('Route not found!');
|
||||
}
|
||||
|
||||
private function echoException(\Exception $exception):void{
|
||||
echo $this->core->getTwig()->render('frames/exception.html.twig', [
|
||||
'message' => $exception->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
public function setGet(array $get): void
|
||||
|
@ -1,4 +1,5 @@
|
||||
{% extends "frames/default.html.twig" %}
|
||||
{% set menu_items = [] %}
|
||||
{% block content %}
|
||||
<h1>Error!</h1>
|
||||
<p>{{ message }}</p>
|
||||
|
@ -2,18 +2,18 @@
|
||||
{% block title %}register{% endblock %}
|
||||
{% block content %}
|
||||
<h1>Register</h1>
|
||||
<form>
|
||||
<form method="post">
|
||||
<div class="form-group">
|
||||
<label for="exampleInputEmail1">Email address</label>
|
||||
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
|
||||
<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="exampleInputEmail1">Username</label>
|
||||
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Username">
|
||||
<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="exampleInputPassword1">Password</label>
|
||||
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
|
||||
<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>
|
||||
|
Loading…
Reference in New Issue
Block a user