Skip to content

Commit

Permalink
Applied fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
olotintemitope authored and StyleCIBot committed Mar 24, 2016
1 parent 067f09e commit 34aa5f8
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 191 deletions.
7 changes: 3 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
* @license <https://opensource.org/license/MIT> MIT
*/
require 'vendor/autoload.php';
use \Psr\Http\Message\ResponseInterface as Response;
use \Psr\Http\Message\ServerRequestInterface as Request;
use Illuminate\Database\Capsule\Manager as Capsule;
use Laztopaz\EmojiRestfulAPI\DatabaseConnection;
use Laztopaz\EmojiRestfulAPI\EmojiController;
use Laztopaz\EmojiRestfulAPI\SlimRouteApp;
use Laztopaz\EmojiRestfulAPI\Oauth;
use Laztopaz\EmojiRestfulAPI\SlimRouteApp;

$capsule = new Capsule();
new DatabaseConnection($capsule);
$emoji = new EmojiController(new Oauth());
$app = new SlimRouteApp(new Oauth(), $emoji);
$app->setUpSlimApp()->run();
$app->setUpSlimApp()->run();
4 changes: 1 addition & 3 deletions src/Auth/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @author Temitope Olotin <temitope.olotin@andela.com>
* @license <https://opensource.org/license/MIT> MIT
*/

namespace Laztopaz\EmojiRestfulAPI;

use Firebase\JWT\JWT;
Expand All @@ -20,19 +19,18 @@ public function __invoke(Request $request, Response $response, $next)

try {
if (is_array($authHeader) && !empty($authHeader)) {

$secretKey = base64_decode(getenv('secret'));
$jwt = json_decode($authHeader[0], true);
//decode the JWT using the key from config

$decodedToken = JWT::decode($jwt['jwt'], $secretKey, ['HS512']);

return $next($request, $response);

}
} catch (\Exception $e) {
return $response->withJson(['status' => $e->getMessage()], 401);
}

return $response->withJson(['status'], 401);
}
}
19 changes: 8 additions & 11 deletions src/Auth/Oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
*/
namespace Laztopaz\EmojiRestfulAPI;

use Laztopaz\EmojiRestfulAPI\User;
use \Firebase\JWT\JWT;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

class Oauth {
use Firebase\JWT\JWT;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class Oauth
{
/**
* This method authenticate user and log them in if the supplied
* credentials are valid.
Expand All @@ -38,12 +37,10 @@ public function loginUser(Request $request, Response $response)

return $response->withJson(['status'], 400);
}

}

/**
*
* This method logout the user
* This method logout the user.
*
* @param $args logout
*
Expand All @@ -67,7 +64,7 @@ public function buildAcessToken(array $userData)
$issuedAt = time();
$notBefore = $issuedAt; //Adding 10 seconds
$expire = $notBefore + (float) strtotime('+30 days'); // Adding 30 days expiry date
$serverName = "http://sweatemoji.com/api"; // Retrieve the server name
$serverName = 'http://sweatemoji.com/api'; // Retrieve the server name

/*
*
Expand All @@ -80,7 +77,7 @@ public function buildAcessToken(array $userData)
'nbf' => $notBefore, // Not before
'exp' => $expire, // Expire

'dat' => $userData // User Information retrieved from the database
'dat' => $userData, // User Information retrieved from the database
];

$loadEnv = DatabaseConnection::loadEnv();
Expand Down
58 changes: 15 additions & 43 deletions src/Controller/EmojiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
*/
namespace Laztopaz\EmojiRestfulAPI;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

use Laztopaz\EmojiRestfulAPI\Emoji;
use Laztopaz\EmojiRestfulAPI\Keyword;

use \Firebase\JWT\JWT;
use Firebase\JWT\JWT;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class EmojiController
{
Expand All @@ -21,13 +17,13 @@ public function __construct(Oauth $auth)
{
$this->auth = $auth;
}

/**
* This method list all emojis.
*
* @param $response
*
* @return json $emojis
*
*/
public function listAllEmoji(Response $response)
{
Expand All @@ -37,7 +33,6 @@ public function listAllEmoji(Response $response)
if (count($emojis) > 0) {
return $response
->withJson($this->formatEmoji($emojis), 200);

}

return $response->withJson(['status'], 404);
Expand All @@ -47,13 +42,10 @@ public function listAllEmoji(Response $response)
* This method get a single emoji.
*
* @param $response
*
* @param $args
*
* @return json $emoji
*
*/

public function getSingleEmoji(Response $response, $args)
{
$id = $args['id'];
Expand All @@ -64,7 +56,6 @@ public function getSingleEmoji(Response $response, $args)
if (count($emoji) > 0) {
return $response
->withJson($this->formatEmoji($emoji), 200);

}

return $response->withStatus(404);
Expand All @@ -76,52 +67,45 @@ public function getSingleEmoji(Response $response, $args)
* @param $args
*
* @return json $response;
*
*/
public function createEmoji(Request $request, Response $response)
{
{
$requestParams = $request->getParsedBody();

$emojiKeyword = $requestParams['keywords'];

$userId = $this->getCurrentUserId($request, $response);

if (is_array($requestParams)) {

$created_at = date('Y-m-d h:i:s');

$emoji = Emoji::create(
[
'name' => $requestParams['name'],
'char' => $requestParams['char'],
'created_at' => $created_at,
'category' => $requestParams['category'],
'created_by' => $userId
'name' => $requestParams['name'],
'char' => $requestParams['char'],
'created_at' => $created_at,
'category' => $requestParams['category'],
'created_by' => $userId,
]
);

if ($emoji->id) {
$createdKeyword = $this->createEmojiKeywords($emoji->id, $emojiKeyword);

return $response->withJson($emoji->toArray(), 201);

}

return $response->withStatus(204);
}

}

/**
*
* This method updates an emoji
* This method updates an emoji.
*
* @param $request
*
* @param $response
*
* @return json
*
*/
public function updateEmojiByPutVerb(Request $request, Response $response, $args)
{
Expand All @@ -131,7 +115,7 @@ public function updateEmojiByPutVerb(Request $request, Response $response, $args
$id = $args['id'];

$emoji = Emoji::find($id);

if ($emoji->id) {
$emoji->name = $upateParams['name'];
$emoji->char = $upateParams['char'];
Expand All @@ -140,7 +124,6 @@ public function updateEmojiByPutVerb(Request $request, Response $response, $args
$emoji->save();

return $response->withJson(['status'], 201);

}

return $response->withJson(['status'], 404);
Expand All @@ -151,7 +134,6 @@ public function updateEmojiByPutVerb(Request $request, Response $response, $args
* This method updates an emoji partially.
*
* @param $request
*
* @param $response
*
* @return json
Expand Down Expand Up @@ -180,7 +162,6 @@ public function updateEmojiByPatchVerb(Request $request, Response $response, $ar
* This method deletes an emoji.
*
* @param $request
*
* @param $response
* @param $args
*
Expand All @@ -197,7 +178,6 @@ public function deleteEmoji(Request $request, Response $response, $args)
Keyword::where('emoji_id', '=', $id)->delete();

return $response->withJson(['status'], 204);

}

return $response->withStatus(404);
Expand All @@ -207,14 +187,11 @@ public function deleteEmoji(Request $request, Response $response, $args)
* This method creates emoji keywords.
*
* @param $request
*
* @param $response
*
* @param $args
*
* @return $id
*/

public function createEmojiKeywords($emoji_id, $keywords)
{
if ($keywords) {
Expand Down Expand Up @@ -253,7 +230,7 @@ public function formatEmoji(array $emojis)
}

/**
* This method authenticate and return user id
* This method authenticate and return user id.
*/
public function getCurrentUserId($request, $response)
{
Expand All @@ -262,7 +239,7 @@ public function getCurrentUserId($request, $response)
$jwtoken = $request->getHeader('HTTP_AUTHORIZATION');

try {
if (isset($jwtoken)) {
if (isset($jwtoken)) {
$secretKey = base64_decode(getenv('secret'));

$jwt = json_decode($jwtoken[0], true);
Expand All @@ -274,15 +251,10 @@ public function getCurrentUserId($request, $response)

$userInfo = (array) $tokenInfo['dat'];

return $userInfo['id'];

return $userInfo['id'];
}

} catch (\Exception $e) {
return $response->withJson(['status' => $e->getMessage()], 401);

}

}

}
10 changes: 4 additions & 6 deletions src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
class UserController
{
/**
<<<<<<< HEAD
*
* This method creates a user account
*
=======
* This method creates a user account.
*
>>>>>>> 59d00e685bf02dd02892708a7ff4c53c06c0437a
=======
* This method creates a user account.
*
>>>>>>> 59d00e685bf02dd02892708a7ff4c53c06c0437a
* @param $data
*
* @return int $statuscode
Expand Down
1 change: 0 additions & 1 deletion src/Database/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function createCategory()
$table->string('category_name');
$table->timestamps();
});

}

/**
Expand Down
Loading

0 comments on commit 34aa5f8

Please sign in to comment.