Refactor service definition
parent
fbc45a0557
commit
6a0f72f271
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\Database;
|
||||
use App\Service\JsonResponse;
|
||||
use App\Service\Request;
|
||||
use App\Service\Session;
|
||||
|
||||
/**
|
||||
* Class Controller
|
||||
* @package App\Controller
|
||||
*/
|
||||
class Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Database
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* @var JsonResponse
|
||||
*/
|
||||
protected $jsonResponse;
|
||||
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* @var \App\Service\Security
|
||||
*/
|
||||
protected $security;
|
||||
|
||||
/**
|
||||
* Controller constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = new Database();
|
||||
$this->request = new Request();
|
||||
$this->jsonResponse = new JsonResponse();
|
||||
$this->session = new Session($this->db, $this->jsonResponse);
|
||||
$this->security = $this->session->security;
|
||||
}
|
||||
}
|
|
@ -2,23 +2,14 @@
|
|||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\JsonResponse;
|
||||
|
||||
class DefaultController
|
||||
class DefaultController extends Controller
|
||||
{
|
||||
private $jsonResponse;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->jsonResponse = new JsonResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* API homepage
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
print $this->jsonResponse->create(200, 'Hello! :)');
|
||||
return $this->jsonResponse->create(200, 'Hello! :)');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,6 +17,6 @@ class DefaultController
|
|||
*/
|
||||
public function error()
|
||||
{
|
||||
print $this->jsonResponse->create(404, 'Resource not found.');
|
||||
return $this->jsonResponse->create(404, 'Resource not found.');
|
||||
}
|
||||
}
|
|
@ -4,30 +4,18 @@ namespace App\Controller;
|
|||
|
||||
use App\Repository\SessionRepository;
|
||||
use App\Repository\UserRepository;
|
||||
use App\Service\Database;
|
||||
use App\Service\JsonResponse;
|
||||
use App\Service\Request;
|
||||
use App\Service\Session;
|
||||
|
||||
class SessionController
|
||||
class SessionController extends Controller
|
||||
{
|
||||
private $db;
|
||||
private $jsonResponse;
|
||||
private $sessionRepository;
|
||||
private $request;
|
||||
private $session;
|
||||
private $security;
|
||||
private $userRepository;
|
||||
private $sessionRepository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = new Database();
|
||||
$this->request = new Request();
|
||||
$this->jsonResponse = new JsonResponse();
|
||||
$this->session = new Session($this->db, $this->jsonResponse);
|
||||
$this->security = $this->session->security;
|
||||
$this->sessionRepository = new SessionRepository($this->db, $this->security);
|
||||
parent::__construct();
|
||||
|
||||
$this->userRepository = new UserRepository($this->db);
|
||||
$this->sessionRepository = new SessionRepository($this->db, $this->security);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,15 +26,13 @@ class SessionController
|
|||
$body = $this->request->getContent()->jsonToArray();
|
||||
|
||||
if (empty($body['username']) || empty($body['password'])) {
|
||||
print $this->jsonResponse->create(400, 'Please provide an username and password.');
|
||||
exit();
|
||||
return $this->jsonResponse->create(400, 'Please provide an username and password.');
|
||||
}
|
||||
|
||||
$user = $this->userRepository->findOneByUsername($body['username']);
|
||||
|
||||
if (is_null($user) || !$this->security->passwordVerify($body['password'], $user['password'])) {
|
||||
print $this->jsonResponse->create(403, 'Bad credentials.');
|
||||
exit();
|
||||
return $this->jsonResponse->create(403, 'Bad credentials.');
|
||||
}
|
||||
|
||||
$token = $this->security->generateToken($user['id']);
|
||||
|
@ -56,7 +42,7 @@ class SessionController
|
|||
|
||||
$this->sessionRepository->create($user['id'], $token, $expire_at->format('Y-m-d H:i:s'), $_SERVER['REMOTE_ADDR']);
|
||||
|
||||
print $this->jsonResponse->create(200, 'Welcome ' . $user['name'], [
|
||||
return $this->jsonResponse->create(200, 'Welcome ' . $user['name'], [
|
||||
'token' => $token,
|
||||
'expire_at' => $expire_at,
|
||||
]);
|
||||
|
@ -70,8 +56,7 @@ class SessionController
|
|||
$body = $this->request->getContent()->jsonToArray();
|
||||
|
||||
if (empty($body['username']) || empty($body['email']) || empty($body['password'])) {
|
||||
print $this->jsonResponse->create(400, 'Please provide an username, email and password.');
|
||||
exit();
|
||||
return $this->jsonResponse->create(400, 'Please provide an username, email and password.');
|
||||
}
|
||||
|
||||
$user = [
|
||||
|
@ -81,13 +66,12 @@ class SessionController
|
|||
];
|
||||
|
||||
if (!is_null($this->userRepository->findOneByEmail($user['email']))) {
|
||||
print $this->jsonResponse->create(403, 'Email already registered!');
|
||||
exit();
|
||||
return $this->jsonResponse->create(403, 'Email already registered!');
|
||||
}
|
||||
|
||||
$this->userRepository->create($user['username'], $user['email'], $user['password']);
|
||||
|
||||
print $this->jsonResponse->create(200, 'Success. Now send your credentials to /auth to sign in.', [
|
||||
return $this->jsonResponse->create(200, 'Success. Now send your credentials to /auth to sign in.', [
|
||||
'username' => $user['username'],
|
||||
'email' => $user['email'],
|
||||
]);
|
||||
|
@ -99,13 +83,12 @@ class SessionController
|
|||
public function signout()
|
||||
{
|
||||
if (!$this->security->isLogged()) {
|
||||
print $this->security->NotAllowedRequest();
|
||||
exit();
|
||||
return $this->security->NotAllowedRequest();
|
||||
}
|
||||
|
||||
$this->sessionRepository->deleteByToken($this->security->getBearerToken());
|
||||
|
||||
print $this->jsonResponse->create(200, 'Good bye.', []);
|
||||
return $this->jsonResponse->create(200, 'Good bye.', []);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -114,10 +97,9 @@ class SessionController
|
|||
public function me()
|
||||
{
|
||||
if (!$this->security->isLogged()) {
|
||||
print $this->security->NotAllowedRequest();
|
||||
exit();
|
||||
return $this->security->NotAllowedRequest();
|
||||
}
|
||||
|
||||
print $this->jsonResponse->create(200, 'hello!', $this->session->getUser());
|
||||
return $this->jsonResponse->create(200, 'hello!', $this->session->getUser());
|
||||
}
|
||||
}
|
|
@ -2,33 +2,21 @@
|
|||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\JsonResponse;
|
||||
use App\Service\Database;
|
||||
use App\Repository\TaskRepository;
|
||||
use App\Service\Request;
|
||||
use App\Service\Session;
|
||||
|
||||
/**
|
||||
* Class TaskController
|
||||
* @package App\Controller
|
||||
*/
|
||||
class TaskController
|
||||
class TaskController extends Controller
|
||||
{
|
||||
private $db;
|
||||
private $request;
|
||||
private $jsonResponse;
|
||||
private $session;
|
||||
private $security;
|
||||
private $repository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = new Database();
|
||||
$this->request = new Request();
|
||||
$this->jsonResponse = new JsonResponse();
|
||||
parent::__construct();
|
||||
|
||||
$this->repository = new TaskRepository($this->db);
|
||||
$this->session = new Session($this->db, $this->jsonResponse);
|
||||
$this->security = $this->session->security;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,7 +31,7 @@ class TaskController
|
|||
$message = "Here are the tasks!";
|
||||
$data = $this->repository->findAll();
|
||||
|
||||
print $this->jsonResponse->create($code, $message, $data);
|
||||
return $this->jsonResponse->create($code, $message, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,7 +46,7 @@ class TaskController
|
|||
$code = ($data != null) ? 200 : 404;
|
||||
$message = ($data != null) ? "Task found." : "Task not found.";
|
||||
|
||||
print $this->jsonResponse->create($code, $message, $data);
|
||||
return $this->jsonResponse->create($code, $message, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -70,8 +58,7 @@ class TaskController
|
|||
public function post()
|
||||
{
|
||||
if (!$this->security->isLogged()) {
|
||||
print $this->security->NotAllowedRequest();
|
||||
exit();
|
||||
return $this->security->NotAllowedRequest();
|
||||
}
|
||||
|
||||
$body = $this->request->getContent()->jsonToArray();
|
||||
|
@ -80,8 +67,7 @@ class TaskController
|
|||
$code = 400;
|
||||
$message = 'Bad parameters.';
|
||||
|
||||
print $this->jsonResponse->create($code, $message);
|
||||
exit();
|
||||
return $this->jsonResponse->create($code, $message);
|
||||
}
|
||||
|
||||
$user = $this->session->getUser();
|
||||
|
@ -97,7 +83,7 @@ class TaskController
|
|||
$message = 'Success!';
|
||||
$data = $task;
|
||||
|
||||
print $this->jsonResponse->create($code, $message, $data);
|
||||
return $this->jsonResponse->create($code, $message, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,16 +95,14 @@ class TaskController
|
|||
public function put($id)
|
||||
{
|
||||
if (!$this->security->isLogged()) {
|
||||
print $this->security->NotAllowedRequest();
|
||||
exit();
|
||||
return $this->security->NotAllowedRequest();
|
||||
}
|
||||
|
||||
$task = $this->repository->findOneById($id);
|
||||
$user = $this->session->getUser();
|
||||
|
||||
if ($task['user_id'] !== $user['id']) {
|
||||
print $this->security->NotAllowedRequest();
|
||||
exit();
|
||||
return $this->security->NotAllowedRequest();
|
||||
}
|
||||
|
||||
$body = $this->request->getContent()->jsonToArray();
|
||||
|
@ -133,7 +117,7 @@ class TaskController
|
|||
$message = "Task edited.";
|
||||
$data = $task;
|
||||
|
||||
print $this->jsonResponse->create($code, $message, $data);
|
||||
return $this->jsonResponse->create($code, $message, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,16 +129,14 @@ class TaskController
|
|||
public function delete($id)
|
||||
{
|
||||
if (!$this->security->isLogged()) {
|
||||
print $this->security->NotAllowedRequest();
|
||||
exit();
|
||||
return $this->security->NotAllowedRequest();
|
||||
}
|
||||
|
||||
$task = $this->repository->findOneById($id);
|
||||
$user = $this->session->getUser();
|
||||
|
||||
if ($task['user_id'] !== $user['id']) {
|
||||
print $this->security->NotAllowedRequest();
|
||||
exit();
|
||||
return $this->security->NotAllowedRequest();
|
||||
}
|
||||
|
||||
$this->repository->deleteById($id);
|
||||
|
@ -163,6 +145,6 @@ class TaskController
|
|||
$message = "Task deleted.";
|
||||
$data = [];
|
||||
|
||||
print $this->jsonResponse->create($code, $message, $data);
|
||||
return $this->jsonResponse->create($code, $message, $data);
|
||||
}
|
||||
}
|
|
@ -3,21 +3,17 @@
|
|||
namespace App\Controller;
|
||||
|
||||
use App\Repository\TaskRepository;
|
||||
use App\Service\Database;
|
||||
use App\Service\JsonResponse;
|
||||
use App\Repository\UserRepository;
|
||||
|
||||
class UserController
|
||||
class UserController extends Controller
|
||||
{
|
||||
private $db;
|
||||
private $jsonResponse;
|
||||
private $repository;
|
||||
private $taskRepository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = new Database();
|
||||
$this->jsonResponse = new JsonResponse();
|
||||
parent::__construct();
|
||||
|
||||
$this->repository = new UserRepository($this->db);
|
||||
$this->taskRepository = new TaskRepository($this->db);
|
||||
}
|
||||
|
@ -34,7 +30,7 @@ class UserController
|
|||
$code = ($user != null) ? 200 : 404;
|
||||
$message = ($user != null) ? "User found." : "User not found.";
|
||||
|
||||
print $this->jsonResponse->create($code, $message, [
|
||||
return $this->jsonResponse->create($code, $message, [
|
||||
'id' => $user['id'],
|
||||
'username' => $user['name'],
|
||||
'email' => $user['email'],
|
||||
|
@ -49,12 +45,11 @@ class UserController
|
|||
$code = ($data != null) ? 200 : 404;
|
||||
$message = ($data != null) ? "User found." : "User not found.";
|
||||
|
||||
print $this->jsonResponse->create($code, $message, []);
|
||||
exit();
|
||||
return $this->jsonResponse->create($code, $message, []);
|
||||
}
|
||||
|
||||
$tasks = $this->taskRepository->findByUserId($id);
|
||||
|
||||
print $this->jsonResponse->create(200, 'Here are the tasks.', $tasks);
|
||||
return $this->jsonResponse->create(200, 'Here are the tasks.', $tasks);
|
||||
}
|
||||
}
|
|
@ -30,7 +30,7 @@ class SessionRepository
|
|||
* TaskRepository constructor.
|
||||
* @param $db
|
||||
*/
|
||||
public function __construct($db, Security $security)
|
||||
public function __construct(Database $db, Security $security)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->security = $security;
|
||||
|
|
|
@ -64,7 +64,7 @@ class UserRepository
|
|||
public function findOneByUsername($username)
|
||||
{
|
||||
$stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' WHERE name = :username');
|
||||
$stmt->bindParam(':username', $username, \PDO::PARAM_INT);
|
||||
$stmt->bindParam(':username', $username, \PDO::PARAM_STR);
|
||||
$stmt->execute();
|
||||
|
||||
$user = $stmt->fetch(\PDO::FETCH_ASSOC);
|
||||
|
@ -83,7 +83,7 @@ class UserRepository
|
|||
public function findOneByEmail($email)
|
||||
{
|
||||
$stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' WHERE email = :email');
|
||||
$stmt->bindParam(':email', $email, \PDO::PARAM_INT);
|
||||
$stmt->bindParam(':email', $email, \PDO::PARAM_STR);
|
||||
$stmt->execute();
|
||||
|
||||
$user = $stmt->fetch(\PDO::FETCH_ASSOC);
|
||||
|
@ -103,9 +103,9 @@ class UserRepository
|
|||
public function create($username, $email, $password)
|
||||
{
|
||||
$stmt = $this->db->getConnection()->prepare('INSERT INTO ' . $this->tableName . ' (`name`, `email`, `password`) VALUES(:name, :email, :password)');
|
||||
$stmt->bindParam(':name', $username);
|
||||
$stmt->bindParam(':email', $email);
|
||||
$stmt->bindParam(':password', $password);
|
||||
$stmt->bindParam(':name', $username, \PDO::PARAM_STR);
|
||||
$stmt->bindParam(':email', $email, \PDO::PARAM_STR);
|
||||
$stmt->bindParam(':password', $password, \PDO::PARAM_STR);
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ namespace App\Service;
|
|||
*/
|
||||
class JsonResponse
|
||||
{
|
||||
public function create(int $code, string $message = null, array $data = [])
|
||||
public function create(int $code, string $message = null, array $data = []): ?string
|
||||
{
|
||||
$response = [
|
||||
'code' => $code,
|
||||
|
@ -16,11 +16,16 @@ class JsonResponse
|
|||
'data' => $data
|
||||
];
|
||||
|
||||
header('Access-Control-Allow-Origin: ' . getenv('ALLOW_ORIGIN'));
|
||||
//header('Access-Control-Allow-Origin: ' . getenv('ALLOW_ORIGIN'));
|
||||
header("Access-Control-Allow-Origin: http://localhost:3000");
|
||||
header("Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token, Authorization");
|
||||
header("Access-Control-Allow-Credentials: true");
|
||||
header('Access-Control-Max-Age: 1');
|
||||
header('Accept: application/json');
|
||||
header('Content-Type: application/json');
|
||||
http_response_code($code);
|
||||
|
||||
return json_encode($response);
|
||||
print json_encode($response);
|
||||
exit();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue