mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-01 00:53:10 +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()]);
|
||||
}
|
||||
} else {
|
||||
$this->render('user/login.html.twig');
|
||||
}
|
||||
@ -58,7 +51,35 @@ final class User extends AbstractDefaultController implements UserInterface
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
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,7 +14,8 @@ use controller\order\Order;
|
||||
*/
|
||||
final class Router implements RouterInterface
|
||||
{
|
||||
const CONTROLLER='controller';
|
||||
|
||||
const CONTROLLER = 'controller';
|
||||
|
||||
const ACTION = 'action';
|
||||
|
||||
@ -40,6 +41,11 @@ final class Router implements RouterInterface
|
||||
*/
|
||||
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) {
|
||||
switch ($this->get[self::CONTROLLER]) {
|
||||
case 'user':
|
||||
@ -56,11 +62,11 @@ final class Router implements RouterInterface
|
||||
$productController = new Product($this->core);
|
||||
switch ($this->get[self::ACTION]) {
|
||||
case 'list':
|
||||
return $productController->list(($this->get['color'])?$this->get['color']:null);
|
||||
return $productController->list(($this->get['color']) ? $this->get['color'] : null);
|
||||
}
|
||||
case 'order':
|
||||
$orderController = new Order($this->core);
|
||||
switch ($this->get[self::ACTION]){
|
||||
switch ($this->get[self::ACTION]) {
|
||||
case 'store':
|
||||
return $orderController->store();
|
||||
case 'basket':
|
||||
@ -76,6 +82,15 @@ final class Router implements RouterInterface
|
||||
return $standartController->homepage();
|
||||
}
|
||||
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
|
||||
|
@ -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