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
{ {
if ($this->post && $this->validateRegistrationData()) {
$this->registerRoutine();
} else {
$this->render('user/register.html.twig'); $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,7 +14,8 @@ 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';
@ -40,6 +41,11 @@ final class Router implements RouterInterface
*/ */
public function route() public function route()
{ {
/**
* 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) { if ($this->get) {
switch ($this->get[self::CONTROLLER]) { switch ($this->get[self::CONTROLLER]) {
case 'user': case 'user':
@ -56,11 +62,11 @@ final class Router implements RouterInterface
$productController = new Product($this->core); $productController = new Product($this->core);
switch ($this->get[self::ACTION]) { switch ($this->get[self::ACTION]) {
case 'list': case 'list':
return $productController->list(($this->get['color'])?$this->get['color']:null); return $productController->list(($this->get['color']) ? $this->get['color'] : null);
} }
case 'order': case 'order':
$orderController = new Order($this->core); $orderController = new Order($this->core);
switch ($this->get[self::ACTION]){ switch ($this->get[self::ACTION]) {
case 'store': case 'store':
return $orderController->store(); return $orderController->store();
case 'basket': case 'basket':
@ -76,6 +82,15 @@ final class Router implements RouterInterface
return $standartController->homepage(); return $standartController->homepage();
} }
throw new \Exception('Route not found!'); throw new \Exception('Route not found!');
} catch (\Exception $exception) {
$this->echoException($exception);
}
}
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>