Skip to content

Commit b925719

Browse files
committed
Refactoring: Move the HTTP method logic to user routes
1 parent 33d3f27 commit b925719

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/Route/user.routes.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
22
namespace PH7\ApiSimpleMenu\Route;
33

4+
use PH7\ApiSimpleMenu\Route\Exception\NotFoundException;
45
use PH7\ApiSimpleMenu\Service\User;
56
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
67

78
use PH7\JustHttp\StatusCode;
8-
use PH7\PhpHttpResponseHeader\Http;
9+
use PH7\PhpHttpResponseHeader\Http as HttpResponse;
910

1011
enum UserAction: string
1112
{
@@ -28,16 +29,29 @@ public function getResponse(): string
2829

2930
$user = new User();
3031
try {
32+
// check first if HTTP method for the requested endpoint is valid
33+
$expectHttpMethod = match ($this) {
34+
self::CREATE => Http::POST_METHOD,
35+
self::UPDATE => Http::POST_METHOD,
36+
self::RETRIEVE_ALL => Http::GET_METHOD,
37+
self::RETRIEVE => Http::GET_METHOD,
38+
self::REMOVE => Http::DELETE_METHOD
39+
};
40+
41+
if (Http::doesHttpMethodMatch($expectHttpMethod) === false) {
42+
throw new NotFoundException('HTTP method is incorrect. Request not found');
43+
}
44+
3145
$response = match ($this) {
3246
self::CREATE => $user->create($postBody),
47+
self::UPDATE => $user->update($postBody),
3348
self::RETRIEVE_ALL => $user->retrieveAll(),
3449
self::RETRIEVE => $user->retrieve($userId),
3550
self::REMOVE => $user->remove($postBody),
36-
self::UPDATE => $user->update($postBody),
3751
};
3852
} catch (InvalidValidationException $e) {
3953
// Send 400 http status code
40-
Http::setHeadersByCode(StatusCode::BAD_REQUEST);
54+
HttpResponse::setHeadersByCode(StatusCode::BAD_REQUEST);
4155

4256
$response = [
4357
'errors' => [

src/Service/User.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,20 @@
22
namespace PH7\ApiSimpleMenu\Service;
33

44
use PH7\ApiSimpleMenu\Dal\UserDal;
5-
use PH7\ApiSimpleMenu\Route\Exception\NotFoundException;
65
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
76
use PH7\ApiSimpleMenu\Validation\UserValidation;
87
use PH7\JustHttp\StatusCode;
98
use PH7\PhpHttpResponseHeader\Http as HttpResponse;
109
use Ramsey\Uuid\Uuid;
1110
use Respect\Validation\Validator as v;
1211
use PH7\ApiSimpleMenu\Entity\User as UserEntity;
13-
use PH7\ApiSimpleMenu\Route\Http;
1412

1513
class User
1614
{
1715
public const DATE_TIME_FORMAT = 'Y-m-d H:i:s';
1816

1917
public function create(mixed $data): array|object
2018
{
21-
if (!Http::doesHttpMethodMatch(Http::POST_METHOD)) {
22-
throw new NotFoundException('HTTP method is incorrect. Request not found');
23-
}
24-
2519
$userValidation = new UserValidation($data);
2620
if ($userValidation->isCreationSchemaValid()) {
2721
$userUuid = Uuid::uuid4()->toString(); // assigning a UUID to the user
@@ -55,10 +49,6 @@ public function create(mixed $data): array|object
5549

5650
public function update(mixed $postBody): array|object
5751
{
58-
if (!Http::doesHttpMethodMatch(Http::POST_METHOD)) {
59-
throw new NotFoundException('HTTP method is incorrect. Request not found');
60-
}
61-
6252
$userValidation = new UserValidation($postBody);
6353
if ($userValidation->isUpdateSchemaValid()) {
6454
$userUuid = $postBody->userUuid;
@@ -123,10 +113,6 @@ public function retrieve(string $userUuid): array
123113
*/
124114
public function remove(mixed $data): bool
125115
{
126-
if (!Http::doesHttpMethodMatch(Http::DELETE_METHOD)) {
127-
throw new NotFoundException('HTTP method is incorrect. Request not found');
128-
}
129-
130116
$userValidation = new UserValidation($data);
131117
if ($userValidation->isRemoveSchemaValid()) {
132118
// Send a 204 if the user got removed

0 commit comments

Comments
 (0)