Skip to content

Commit

Permalink
Merge pull request #104 from maurobonfietti/1.13.0
Browse files Browse the repository at this point in the history
Version 1.13.0
  • Loading branch information
maurobonfietti authored Jul 31, 2020
2 parents 98962a4 + b392a35 commit b30326c
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 58 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# REST API IN SLIM PHP

Example of REST API with [Slim PHP micro framework](https://www.slimframework.com).
Example of RESTful API with [Slim PHP micro framework](https://www.slimframework.com).

![alt text](extras/img/slim-logo.png "Slim PHP micro framework")

This simple RESTful API, allows CRUD operations to manage resources like: Users, Tasks and Notes.
This simple API allows you to manage resources such as: users, tasks and notes.

[![Software License][ico-license]](LICENSE.md)
[![Build Status](https://travis-ci.org/maurobonfietti/rest-api-slim-php.svg?branch=master)](https://travis-ci.org/maurobonfietti/rest-api-slim-php)
Expand Down
6 changes: 2 additions & 4 deletions README_SPANISH.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# API REST EN SLIM PHP

Ejemplo de API REST con micro framework [Slim PHP](https://www.slimframework.com).
Ejemplo de API RESTful con micro framework [Slim PHP](https://www.slimframework.com).

![alt text](extras/img/slim-logo.png "Slim PHP micro framework")

Esta simple API RESTful, permite operaciones CRUD para administrar recursos como por ejemplo: Usuarios, Tareas y Notas.
Esta simple API permite administrar recursos tales como: usuarios, tareas y notas.

[![Software License][ico-license]](LICENSE.md)
[![Build Status](https://travis-ci.org/maurobonfietti/rest-api-slim-php.svg?branch=master)](https://travis-ci.org/maurobonfietti/rest-api-slim-php)
Expand Down
26 changes: 1 addition & 25 deletions extras/bin/post-create-project-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

declare(strict_types=1);

final class PostCreateProjectCommand
{
public static function showIntro(): void
{
echo <<<EOF
echo <<<EOF
_ _
| | (_)
_ __ ___ ___| |_ __ _ _ __ _
Expand All @@ -23,26 +19,12 @@ public static function showIntro(): void
| | | |
|_| |_|
EOF;
}

public static function showSuccessMessage(): void
{
echo <<<EOF
*************************************************************
Project: https://github.com/maurobonfietti/rest-api-slim-php
*************************************************************
Successfully created project!
EOF;
}

public static function showFinalMessage(): void
{
echo <<<EOF
Get started with the following commands:
$ cd [my-api-name]
Expand All @@ -57,9 +39,3 @@ public static function showFinalMessage(): void
Now go build a cool RESTful API ;-)
EOF;
}
}

PostCreateProjectCommand::showIntro();
PostCreateProjectCommand::showSuccessMessage();
PostCreateProjectCommand::showFinalMessage();
Binary file removed extras/img/slim-logo.png
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

abstract class BaseController
{
protected $container;

protected const DEFAULT_PER_PAGE_PAGINATION = 5;

protected $container;

public function __construct(Container $container)
{
$this->container = $container;
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class DefaultController extends BaseController
{
public const API_VERSION = '1.12.0';
public const API_VERSION = '1.13.0';

public function getHelp(Request $request, Response $response): Response
{
Expand Down
7 changes: 5 additions & 2 deletions src/Controller/Note/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

final class Delete extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$this->getServiceDeleteNote()->delete((int) $args['id']);

return $this->jsonResponse($response, 'success', null, 204);
Expand Down
7 changes: 5 additions & 2 deletions src/Controller/Note/GetAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

final class GetAll extends Base
{
public function __invoke(Request $request, Response $response): Response
{
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$page = $request->getQueryParam('page', null);
$perPage = $request->getQueryParam('perPage', self::DEFAULT_PER_PAGE_PAGINATION);

Expand Down
7 changes: 5 additions & 2 deletions src/Controller/Note/GetOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

final class GetOne extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$note = $this->getServiceFindNote()->getOne((int) $args['id']);

return $this->jsonResponse($response, 'success', $note, 200);
Expand Down
7 changes: 5 additions & 2 deletions src/Controller/Note/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

final class Search extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$notes = $this->getServiceFindNote()->search($args['query']);

return $this->jsonResponse($response, 'success', $notes, 200);
Expand Down
7 changes: 5 additions & 2 deletions src/Controller/Note/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

final class Update extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$input = (array) $request->getParsedBody();
$note = $this->getServiceUpdateNote()->update($input, (int) $args['id']);

Expand Down
12 changes: 2 additions & 10 deletions src/Middleware/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@

abstract class Base
{
private const FORBIDDEN_MESSAGE = 'Forbidden: you are not authorized.';

protected function checkToken(string $token): object
{
try {
$decoded = JWT::decode($token, getenv('SECRET_KEY'), ['HS256']);
if (is_object($decoded) && isset($decoded->sub)) {
return $decoded;
}
throw new Auth(self::FORBIDDEN_MESSAGE, 403);
return JWT::decode($token, getenv('SECRET_KEY'), ['HS256']);
} catch (\UnexpectedValueException $exception) {
throw new Auth(self::FORBIDDEN_MESSAGE, 403);
} catch (\DomainException $exception) {
throw new Auth(self::FORBIDDEN_MESSAGE, 403);
throw new Auth('Forbidden: you are not authorized.', 403);
}
}
}
2 changes: 1 addition & 1 deletion src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function getDb(): \PDO
protected function getResultByPage($query, $page, $perPage)
{
$offset = ($page - 1) * $perPage;
$query = $query . " LIMIT $perPage OFFSET $offset";
$query .= " LIMIT ${perPage} OFFSET ${offset}";
$statement = $this->database->prepare($query);
$statement->execute();

Expand Down
2 changes: 1 addition & 1 deletion src/Repository/NoteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getNotes(): array

public function getNotesByPage($page, $perPage): array
{
$query = "SELECT * FROM `notes`";
$query = 'SELECT * FROM `notes`';
$statement = $this->database->prepare($query);
$statement->execute();
$total = $statement->rowCount();
Expand Down

0 comments on commit b30326c

Please sign in to comment.