Skip to content

Commit 6b5eca3

Browse files
committed
Cleanup: Handle nicely errors without anti-patterns
1 parent 72cd8a1 commit 6b5eca3

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
"config": {
1717
"platform": {
18-
"php": "8.1.0"
18+
"php": "8.2.0"
1919
}
2020
},
2121
"require": {
22-
"php": ">=8.1.0",
22+
"php": ">=8.2.0",
2323
"firebase/php-jwt": "^6.4",
2424
"respect/validation": "^2.2",
2525
"ph-7/php-http-response-header": "^1.0.2",

src/Dal/UserDal.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
use PH7\ApiSimpleMenu\Entity\User as UserEntity;
66
use RedBeanPHP\R;
7+
use RedBeanPHP\RedException\SQL;
78

89
final class UserDal
910
{
1011
public const TABLE_NAME = 'users';
1112

12-
/**
13-
* @throws \RedBeanPHP\RedException\SQL
14-
*/
15-
public static function create(UserEntity $userEntity): int|string
13+
public static function create(UserEntity $userEntity): int|string|false
1614
{
1715
$userBean = R::dispense(self::TABLE_NAME);
1816
$userBean->user_uuid = $userEntity->getUserUuid();
@@ -22,14 +20,18 @@ public static function create(UserEntity $userEntity): int|string
2220
$userBean->phone = $userEntity->getPhone();
2321
$userBean->created_date = $userEntity->getCreationDate();
2422

25-
$id = R::store($userBean);
26-
27-
R::close();
23+
try {
24+
$id = R::store($userBean);
25+
} catch (SQL $e) {
26+
return false;
27+
} finally {
28+
R::close();
29+
}
2830

2931
return $id;
3032
}
3133

32-
public static function update(string $userUuid, UserEntity $userEntity): int|string
34+
public static function update(string $userUuid, UserEntity $userEntity): int|string|false
3335
{
3436
$userBean = R::findOne(self::TABLE_NAME, 'user_uuid = :userUuid', ['userUuid' => $userUuid]);
3537

@@ -52,10 +54,16 @@ public static function update(string $userUuid, UserEntity $userEntity): int|str
5254
}
5355

5456
// save the user
55-
return R::store($userBean);
57+
try {
58+
return R::store($userBean);
59+
} catch (SQL $e) {
60+
return false;
61+
} finally {
62+
R::close();
63+
}
5664
}
5765

58-
return 0;
66+
return false;
5967
}
6068

6169
public static function get(string $userUuid): ?array

src/Service/User.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PH7\JustHttp\StatusCode;
88
use PH7\PhpHttpResponseHeader\Http;
99
use Ramsey\Uuid\Uuid;
10-
use RedBeanPHP\RedException\SQL;
1110
use Respect\Validation\Validator as v;
1211
use PH7\ApiSimpleMenu\Entity\User as UserEntity;
1312

@@ -30,9 +29,8 @@ public function create(mixed $data): array|object
3029
->setPhone($data->phone)
3130
->setCreationDate(date(self::DATE_TIME_FORMAT));
3231

33-
try {
34-
UserDal::create($userEntity);
35-
} catch (SQL $exception) {
32+
33+
if (UserDal::create($userEntity) === false) {
3634
// Set an internal error when we cannot add an entry to the database
3735
Http::setHeadersByCode(StatusCode::INTERNAL_SERVER_ERROR);
3836

@@ -65,13 +63,12 @@ public function update(mixed $postBody): array|object
6563
$userEntity->setPhone($postBody->phone);
6664
}
6765

68-
$result = UserDal::update($userUuid, $userEntity);
69-
if ($result) {
70-
return $postBody;
66+
if (UserDal::update($userUuid, $userEntity) === false) {
67+
// If invalid or got an error, give back an empty response
68+
return [];
7169
}
7270

73-
// if invalid, give back an empty response
74-
return [];
71+
return $postBody;
7572
}
7673

7774
throw new InvalidValidationException("Invalid user payload");

0 commit comments

Comments
 (0)