Skip to content

Commit 5332aed

Browse files
perf: remove all check functions
1 parent ce9c673 commit 5332aed

File tree

5 files changed

+23
-34
lines changed

5 files changed

+23
-34
lines changed

src/Controller/AbstractAuthController.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Firebase\JWT\JWT;
66
use Firebase\JWT\Key;
77
use RestJS\Message\Response;
8-
use function RestJS\errorHandler;
98
use function RestJS\response;
109
use Slim\Exception\HttpBadRequestException;
1110
use Slim\Exception\HttpUnauthorizedException;
@@ -30,7 +29,7 @@ public function login($req, $res) {
3029
throw new HttpBadRequestException($req, "Username or password is required.");
3130

3231
/** Check User Entity */
33-
$user = errorHandler($this->_model->filter(['username' => $username]));
32+
$user = $this->_model->filter(['username' => $username]);
3433

3534
if (!$user)
3635
throw new HttpUnauthorizedException($req, 'Invalid user credentials');
@@ -74,11 +73,8 @@ public function regenerateAccessToken($req, $res) {
7473
/** Decode Json Web Token */
7574
$decodedToken = (array) JWT::decode($refreshToken, new Key($_ENV['REFRESH_TOKEN_SECRET'], 'HS256'));
7675
} catch (\Exception $e) {
77-
$decodedToken = null;
76+
throw new HttpUnauthorizedException($req, "Invalid access token");
7877
}
79-
80-
if (!$decodedToken)
81-
throw new HttpUnauthorizedException($req, "Invalid access token");
8278

8379
/** Check User Entity */
8480
$user = $this->_model->findById($decodedToken['id']);

src/Controller/AbstractController.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace RestJS\Controller;
44

55
use RestJS\Message\Response;
6-
use function RestJS\response, RestJS\checkNull;
6+
use function RestJS\response;
77
use Slim\Exception\HttpBadRequestException;
88

99
/** Abstract Controller Functions */
@@ -30,13 +30,19 @@ public function findByColumn($req, $res, $args) {
3030
/** Delete Data by Id */
3131
public function delete($req, $res, $args) {
3232
$data = $this->_model->delete($args);
33-
checkNull($data, $req);
33+
34+
// Check data is deleted
35+
if (!$data)
36+
throw new HttpBadRequestException($req, "Something went wrong...");
37+
3438
return response($req, $res, new Response(message: "This item has been successfully removed.", data: $data));
3539
}
3640

3741
/** Insert Data */
3842
public function insert($req, $res, $args) {
39-
if (!$req->getParsedBody())
43+
44+
// Check user input valid form data
45+
if(!$req->getParsedBody())
4046
throw new HttpBadRequestException($req, "Please enter valid form data.");
4147

4248
$data = $this->_model->insert([...$req->getParsedBody(), ...$args]);
@@ -45,11 +51,17 @@ public function insert($req, $res, $args) {
4551

4652
/** Update by Id */
4753
public function update($req, $res, $args) {
54+
55+
// check user input valid form data
4856
if (!$req->getParsedBody())
4957
throw new HttpBadRequestException($req, "Please enter valid form data.");
5058

5159
$data = $this->_model->update($req->getParsedBody(), $args);
52-
checkNull($data, $req);
60+
61+
// Check data is updated
62+
if (!$data)
63+
throw new HttpBadRequestException($req, "Something went wrong...");
64+
5365
return response($req, $res, new Response(message: "This item has been successfully updated.", data: $data));
5466
}
5567
}

src/Middleware/AbstractAuthMiddleware.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ public function process(Request $req, RequestHandler $handler): ResponseInterfac
3131
try {
3232
/** Decode Json Web Token */
3333
$decodedToken = (array) JWT::decode($token, new Key($_ENV['ACCESS_TOKEN_SECRET'], 'HS256'));
34-
}
35-
catch (\Exception $e) {
36-
$decodedToken = null;
37-
}
38-
39-
if (!$decodedToken)
34+
} catch (\Exception $e) {
4035
throw new HttpUnauthorizedException($req, "Invalid access token");
41-
36+
}
37+
4238
/** Check User Entity */
4339
$user = $this->_user->findById($decodedToken['id']);
4440

src/Model/AbstractModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public function findBy(array $args): array {
3232
}
3333

3434
/** Filter Data by Conditional */
35-
public function filter(array $args): object {
35+
public function filter(array $args): object|null {
3636
return $this->repository->findOneBy($args);
3737
}
3838

3939
/** Find Data by Id */
40-
public function findById($id): object {
40+
public function findById($id): object|null {
4141
return $this->repository->find($id);
4242
}
4343

src/function.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,11 @@
22
declare(strict_types=1);
33
namespace RestJS;
44

5-
use Slim\Exception\HttpBadRequestException;
65
use Psr\Http\Message\ResponseInterface as Response;
76
use Psr\Http\Message\ServerRequestInterface as Request;
87

98
/** Query Response Function */
109
function response(Request $req, Response $res, mixed $args) {
1110
$res->getBody()->write(json_encode($args));
1211
return $res;
13-
}
14-
15-
/** Check Null Value Function */
16-
function checkNull($value, $req) {
17-
!boolval($value) && throw new HttpBadRequestException($req, "Something went wrong...");
18-
}
19-
20-
/** Callback Error Handler Function */
21-
function errorHandler($callback) {
22-
try {
23-
return $callback;
24-
} catch (\Exception $e) {
25-
return null;
26-
}
2712
}

0 commit comments

Comments
 (0)