Skip to content

Commit

Permalink
Merge pull request #50 from maurobonfietti/v0.27.0
Browse files Browse the repository at this point in the history
Release Version 0.27.0
  • Loading branch information
maurobonfietti authored Aug 20, 2019
2 parents 06c6b01 + 5773718 commit 9e3bade
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 114 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ DB_PASSWORD=''

DISPLAY_ERROR_DETAILS=true
APP_DOMAIN='https://www.yourdomain.com'
USE_REDIS_CACHE=false
REDIS_URL=''
SECRET_KEY='YourSuperSecret-KeY'
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ env:
- DB_DATABASE=rest_api_slim_php
- DB_USERNAME=root
- DB_PASSWORD=
- USE_REDIS_CACHE=true
- REDIS_URL=localhost
- SECRET_KEY=YourSuperSecret-KeY

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ For example:
```
DISPLAY_ERROR_DETAILS=true
APP_DOMAIN='https://www.yourdomain.com'
USE_REDIS_CACHE=false
REDIS_URL=''
SECRET_KEY='YourSuperSecret-KeY'
```
Expand Down
1 change: 0 additions & 1 deletion README_SPANISH.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ Por ejemplo:
```
DISPLAY_ERROR_DETAILS=true
APP_DOMAIN='https://www.yourdomain.com'
USE_REDIS_CACHE=false
REDIS_URL=''
SECRET_KEY='YourSuperSecret-KeY'
```
Expand Down
1 change: 0 additions & 1 deletion src/App/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
],
'useRedisCache' => filter_var(getenv('USE_REDIS_CACHE'), FILTER_VALIDATE_BOOLEAN),
],
];
10 changes: 0 additions & 10 deletions src/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,4 @@ protected function getInput()
{
return $this->request->getParsedBody();
}

protected function getRedisClient(): \Predis\Client
{
return $this->container->get('redis');
}

protected function useRedis(): bool
{
return $this->container->get('settings')['useRedisCache'];
}
}
3 changes: 2 additions & 1 deletion src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class DefaultController extends BaseController
{
const API_VERSION = '0.26.0';
const API_VERSION = '0.27.0';

public function __construct(Container $container)
{
Expand All @@ -23,6 +23,7 @@ public function getHelp(Request $request, Response $response, array $args): Resp
'tasks' => $url . '/api/v1/tasks',
'users' => $url . '/api/v1/users',
'notes' => $url . '/api/v1/notes',
'docs' => $url . '/docs/index.html',
'status' => $url . '/status',
'this help' => $url . '',
];
Expand Down
5 changes: 0 additions & 5 deletions src/Controller/Note/BaseNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

abstract class BaseNote extends BaseController
{
/**
* @var NoteService
*/
protected $noteService;

public function __construct(Container $container)
{
$this->container = $container;
Expand Down
9 changes: 2 additions & 7 deletions src/Controller/Task/BaseTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@

abstract class BaseTask extends BaseController
{
/**
* @var TaskService
*/
protected $taskService;

public function __construct(Container $container)
{
$this->taskService = $container->get('task_service');
$this->container = $container;
}

protected function getTaskService(): TaskService
{
return $this->taskService;
return $this->container->get('task_service');
}
}
8 changes: 0 additions & 8 deletions src/Controller/User/BaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

abstract class BaseUser extends BaseController
{
/**
* @var UserService
*/
protected $userService;

public function __construct(Container $container)
{
$this->container = $container;
Expand All @@ -24,9 +19,6 @@ protected function getUserService(): UserService
return $this->container->get('user_service');
}

/**
* @throws UserException
*/
protected function checkUserPermissions()
{
$input = $this->getInput();
Expand Down
7 changes: 0 additions & 7 deletions src/Middleware/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@

class AuthMiddleware
{
/**
* @param Request $request
* @param Response $response
* @param Callable $next
* @return ResponseInterface
* @throws AuthException
*/
public function __invoke(Request $request, Response $response, $next): ResponseInterface
{
$jwtHeader = $request->getHeaderLine('Authorization');
Expand Down
3 changes: 0 additions & 3 deletions src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

abstract class BaseRepository
{
/**
* @var \PDO $database
*/
protected $database;

protected function getDb(): \PDO
Expand Down
5 changes: 0 additions & 5 deletions src/Service/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

abstract class BaseService
{
public function useRedis(): bool
{
return filter_var(getenv('USE_REDIS_CACHE'), FILTER_VALIDATE_BOOLEAN);
}

protected static function validateUserName(string $name): string
{
if (!v::alnum()->length(2, 100)->validate($name)) {
Expand Down
25 changes: 8 additions & 17 deletions src/Service/NoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class NoteService extends BaseService
{
/**
* @var NoteRepository
*/
protected $noteRepository;

protected $redisService;
Expand All @@ -32,8 +29,8 @@ public function getNotes(): array

public function getNote(int $noteId)
{
$key = "note:$noteId";
if ($this->useRedis() === true && $this->redisService->exists($key)) {
$key = $this->redisService->generateKey("note:$noteId");
if ($this->redisService->exists($key)) {
$note = $this->redisService->get($key);
} else {
$note = $this->checkAndGetNote($noteId);
Expand Down Expand Up @@ -61,10 +58,8 @@ public function createNote($input)
$note->description = $data->description;
}
$notes = $this->noteRepository->createNote($note);
if ($this->useRedis() === true) {
$key = "note:" . $notes->id;
$this->redisService->setex($key, $notes);
}
$key = $this->redisService->generateKey("note:" . $notes->id);
$this->redisService->setex($key, $notes);

return $notes;
}
Expand All @@ -83,10 +78,8 @@ public function updateNote($input, int $noteId)
$note->description = $data->description;
}
$notes = $this->noteRepository->updateNote($note);
if ($this->useRedis() === true) {
$key = "note:" . $notes->id;
$this->redisService->setex($key, $notes);
}
$key = $this->redisService->generateKey("note:" . $notes->id);
$this->redisService->setex($key, $notes);

return $notes;
}
Expand All @@ -95,9 +88,7 @@ public function deleteNote(int $noteId)
{
$this->checkAndGetNote($noteId);
$this->noteRepository->deleteNote($noteId);
if ($this->useRedis() === true) {
$key = "note:" . $noteId;
$this->redisService->del($key);
}
$key = $this->redisService->generateKey("note:" . $noteId);
$this->redisService->del($key);
}
}
17 changes: 5 additions & 12 deletions src/Service/RedisService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,33 @@ public function __construct($redis)
$this->redis = $redis;
}

private function generateKey($value)
public function generateKey($value)
{
return self::PROJECT_NAME . ':' . $value;
}

public function exists($key)
{
$key = $this->generateKey($key);

return $this->redis->exists($key);
}

public function get($key)
{
$key = $this->generateKey($key);

return json_decode($this->redis->get($key), true);
}

public function set($key, $value)
{
$key = $this->generateKey($key);
$this->redis->set($key, json_encode($value));
}

public function del($key)
public function setex($key, $value, $ttl = 3600)
{
$key = $this->generateKey($key);
$this->redis->del($key);
$this->redis->setex($key, $ttl, json_encode($value));
}

public function setex($key, $value, $ttl = 3600)
public function del($key)
{
$key = $this->generateKey($key);
$this->redis->setex($key, $ttl, json_encode($value));
$this->redis->del($key);
}
}
25 changes: 8 additions & 17 deletions src/Service/TaskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class TaskService extends BaseService
{
/**
* @var TaskRepository
*/
protected $taskRepository;

protected $redisService;
Expand Down Expand Up @@ -42,8 +39,8 @@ public function getTasks(int $userId): array

public function getTask(int $taskId, int $userId)
{
$key = "task:$taskId:user:$userId";
if ($this->useRedis() === true && $this->redisService->exists($key)) {
$key = $this->redisService->generateKey("task:$taskId:user:$userId");
if ($this->redisService->exists($key)) {
$task = $this->redisService->get($key);
} else {
$task = $this->checkAndGetTask($taskId, $userId);
Expand Down Expand Up @@ -80,10 +77,8 @@ public function createTask(array $input)
}
$task->userId = $data->decoded->sub;
$tasks = $this->getTaskRepository()->createTask($task);
if ($this->useRedis() === true) {
$key = "task:" . $tasks->id . ":user:" . $task->userId;
$this->redisService->setex($key, $tasks);
}
$key = $this->redisService->generateKey("task:" . $tasks->id . ":user:" . $task->userId);
$this->redisService->setex($key, $tasks);

return $tasks;
}
Expand All @@ -106,10 +101,8 @@ public function updateTask(array $input, int $taskId)
}
$task->userId = $data->decoded->sub;
$tasks = $this->getTaskRepository()->updateTask($task);
if ($this->useRedis() === true) {
$key = "task:" . $tasks->id . ":user:" . $task->userId;
$this->redisService->setex($key, $tasks);
}
$key = $this->redisService->generateKey("task:" . $tasks->id . ":user:" . $task->userId);
$this->redisService->setex($key, $tasks);

return $tasks;
}
Expand All @@ -118,10 +111,8 @@ public function deleteTask(int $taskId, int $userId): string
{
$this->checkAndGetTask($taskId, $userId);
$data = $this->getTaskRepository()->deleteTask($taskId, $userId);
if ($this->useRedis() === true) {
$key = "task:" . $taskId . ":user:" . $userId;
$this->redisService->del($key);
}
$key = $this->redisService->generateKey("task:" . $taskId . ":user:" . $userId);
$this->redisService->del($key);

return $data;
}
Expand Down
25 changes: 8 additions & 17 deletions src/Service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class UserService extends BaseService
{
/**
* @var UserRepository
*/
protected $userRepository;

protected $redisService;
Expand All @@ -33,8 +30,8 @@ public function getUsers(): array

public function getUser(int $userId)
{
$key = "user:$userId";
if ($this->useRedis() === true && $this->redisService->exists($key)) {
$key = $this->redisService->generateKey("user:$userId");
if ($this->redisService->exists($key)) {
$data = $this->redisService->get($key);
$user = json_decode(json_encode($data), false);
} else {
Expand Down Expand Up @@ -68,10 +65,8 @@ public function createUser($input)
$user->password = hash('sha512', $data->password);
$this->userRepository->checkUserByEmail($user->email);
$users = $this->userRepository->createUser($user);
if ($this->useRedis() === true) {
$key = "user:" . $users->id;
$this->redisService->setex($key, $users);
}
$key = $this->redisService->generateKey("user:" . $users->id);
$this->redisService->setex($key, $users);

return $users;
}
Expand All @@ -90,10 +85,8 @@ public function updateUser(array $input, int $userId)
$user->email = self::validateEmail($data->email);
}
$users = $this->userRepository->updateUser($user);
if ($this->useRedis() === true) {
$key = "user:" . $users->id;
$this->redisService->setex($key, $users);
}
$key = $this->redisService->generateKey("user:" . $users->id);
$this->redisService->setex($key, $users);

return $users;
}
Expand All @@ -103,10 +96,8 @@ public function deleteUser(int $userId): string
$this->checkAndGetUser($userId);
$this->userRepository->deleteUserTasks($userId);
$data = $this->userRepository->deleteUser($userId);
if ($this->useRedis() === true) {
$key = "user:" . $userId;
$this->redisService->del($key);
}
$key = $this->redisService->generateKey("user:" . $userId);
$this->redisService->del($key);

return $data;
}
Expand Down

0 comments on commit 9e3bade

Please sign in to comment.