From 2ec0a668963353197b9acbdf975fd6c8def34667 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sun, 15 Jul 2018 11:35:11 +0200 Subject: [PATCH] Implemented exception --- src/controller/user/User.php | 39 ++++++++--- src/router/Router.php | 89 +++++++++++++++---------- src/template/frames/exception.html.twig | 1 + src/template/user/register.html.twig | 14 ++-- 4 files changed, 90 insertions(+), 53 deletions(-) diff --git a/src/controller/user/User.php b/src/controller/user/User.php index a95472d..1f13fc0 100644 --- a/src/controller/user/User.php +++ b/src/controller/user/User.php @@ -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; } } diff --git a/src/router/Router.php b/src/router/Router.php index dcad083..8947a92 100644 --- a/src/router/Router.php +++ b/src/router/Router.php @@ -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 diff --git a/src/template/frames/exception.html.twig b/src/template/frames/exception.html.twig index fa822e7..275d528 100644 --- a/src/template/frames/exception.html.twig +++ b/src/template/frames/exception.html.twig @@ -1,4 +1,5 @@ {% extends "frames/default.html.twig" %} +{% set menu_items = [] %} {% block content %}

Error!

{{ message }}

diff --git a/src/template/user/register.html.twig b/src/template/user/register.html.twig index b1716e4..2a7f0d5 100644 --- a/src/template/user/register.html.twig +++ b/src/template/user/register.html.twig @@ -2,18 +2,18 @@ {% block title %}register{% endblock %} {% block content %}

Register

-
+
- - + +
- - + +
- - + +