Implemented exception

This commit is contained in:
Kevin Frantz 2018-07-15 11:35:11 +02:00
parent a444f4805c
commit 2ec0a66896
4 changed files with 90 additions and 53 deletions

View File

@ -2,8 +2,6 @@
namespace controller\user; namespace controller\user;
use controller\AbstractDefaultController; use controller\AbstractDefaultController;
use router\Router;
use controller\AbstractController;
use core\CoreInterface; use core\CoreInterface;
use repository\user\User as UserRepository; use repository\user\User as UserRepository;
use entity\user\User as UserEntity; use entity\user\User as UserEntity;
@ -15,7 +13,6 @@ use entity\user\User as UserEntity;
*/ */
final class User extends AbstractDefaultController implements UserInterface final class User extends AbstractDefaultController implements UserInterface
{ {
/** /**
* *
* @var UserRepository * @var UserRepository
@ -37,11 +34,7 @@ final class User extends AbstractDefaultController implements UserInterface
public function login(): void public function login(): void
{ {
if ($this->post) { if ($this->post) {
try { $this->loginRoutine();
$this->loginRoutine();
} catch (\Exception $exception) {
$this->render('frames/exception.html.twig',['message'=>$exception->getMessage()]);
}
} else { } else {
$this->render('user/login.html.twig'); $this->render('user/login.html.twig');
} }
@ -58,7 +51,35 @@ final class User extends AbstractDefaultController implements UserInterface
public function register(): void 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;
} }
} }

View File

@ -14,10 +14,11 @@ use controller\order\Order;
*/ */
final class Router implements RouterInterface final class Router implements RouterInterface
{ {
const CONTROLLER='controller';
const CONTROLLER = 'controller';
const ACTION = 'action'; const ACTION = 'action';
/** /**
* *
* @var CoreInterface * @var CoreInterface
@ -40,42 +41,56 @@ final class Router implements RouterInterface
*/ */
public function route() public function route()
{ {
if ($this->get) { /**
switch ($this->get[self::CONTROLLER]) { * From a security perspective this try catch is not a good idea
case 'user': * It's just here for usability reasons and to save code ;)
$userController = new User($this->core); */
switch ($this->get[self::ACTION]) { try {
case 'login': if ($this->get) {
return $userController->login(); switch ($this->get[self::CONTROLLER]) {
case 'logout': case 'user':
return $userController->logout(); $userController = new User($this->core);
case 'register': switch ($this->get[self::ACTION]) {
return $userController->register(); case 'login':
} return $userController->login();
case 'product': case 'logout':
$productController = new Product($this->core); return $userController->logout();
switch ($this->get[self::ACTION]) { case 'register':
case 'list': return $userController->register();
return $productController->list(($this->get['color'])?$this->get['color']:null); }
} case 'product':
case 'order': $productController = new Product($this->core);
$orderController = new Order($this->core); switch ($this->get[self::ACTION]) {
switch ($this->get[self::ACTION]){ case 'list':
case 'store': return $productController->list(($this->get['color']) ? $this->get['color'] : null);
return $orderController->store(); }
case 'basket': case 'order':
return $orderController->basket(); $orderController = new Order($this->core);
case 'payment': switch ($this->get[self::ACTION]) {
return $orderController->selectPaymentMethod(); case 'store':
case 'add-product': return $orderController->store();
return $orderController->addProduct(); 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 { throw new \Exception('Route not found!');
$standartController = new Standart($this->core); } catch (\Exception $exception) {
return $standartController->homepage(); $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 public function setGet(array $get): void

View File

@ -1,4 +1,5 @@
{% extends "frames/default.html.twig" %} {% extends "frames/default.html.twig" %}
{% set menu_items = [] %}
{% block content %} {% block content %}
<h1>Error!</h1> <h1>Error!</h1>
<p>{{ message }}</p> <p>{{ message }}</p>

View File

@ -2,18 +2,18 @@
{% block title %}register{% endblock %} {% block title %}register{% endblock %}
{% block content %} {% block content %}
<h1>Register</h1> <h1>Register</h1>
<form> <form method="post">
<div class="form-group"> <div class="form-group">
<label for="exampleInputEmail1">Email address</label> <label for="email">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email"> <input type="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="exampleInputEmail1">Username</label> <label for="name">Username</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Username"> <input type="email" 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="exampleInputPassword1">Password</label> <label for="password">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password"> <input type="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>