From 91810190b78a46f08f0fb70bd4bf34d9917ea013 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Thu, 6 Jul 2023 21:54:10 +0600 Subject: [PATCH] Introduced a new class ServiceResultSuccess to distinctively handle successful service responses. Changes were made in AuthService and Service base class to replace ServiceResultArray with ServiceResultSuccess for successful operations. This provides a more accurate response type and improves code readability. --- app/ServiceResults/ServiceResultSuccess.php | 15 +++++++++++++++ app/Services/AuthService.php | 4 ++-- app/Services/Service.php | 5 +++-- 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 app/ServiceResults/ServiceResultSuccess.php diff --git a/app/ServiceResults/ServiceResultSuccess.php b/app/ServiceResults/ServiceResultSuccess.php new file mode 100644 index 0000000..7a6a3c6 --- /dev/null +++ b/app/ServiceResults/ServiceResultSuccess.php @@ -0,0 +1,15 @@ +message; + } +} diff --git a/app/Services/AuthService.php b/app/Services/AuthService.php index 03dc196..6542546 100644 --- a/app/Services/AuthService.php +++ b/app/Services/AuthService.php @@ -4,8 +4,8 @@ namespace App\Services; use App\Dto\Request\Authorization; use App\Repositories\UserRepository; -use App\ServiceResults\ServiceResultArray; use App\ServiceResults\ServiceResultError; +use App\ServiceResults\ServiceResultSuccess; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; @@ -15,7 +15,7 @@ final class AuthService extends Service private readonly UserRepository $userRepository ) { } - public function authorization(Authorization $authorization): ServiceResultError | ServiceResultArray + public function authorization(Authorization $authorization): ServiceResultError | ServiceResultSuccess { $user = $this->userRepository->getUserByEmail($authorization->getEmail()); if (is_null($user)) { diff --git a/app/Services/Service.php b/app/Services/Service.php index d844c35..f7bb5f9 100644 --- a/app/Services/Service.php +++ b/app/Services/Service.php @@ -5,6 +5,7 @@ namespace App\Services; use App\ServiceResults\ServiceResultArray; use App\ServiceResults\ServiceResultError; +use App\ServiceResults\ServiceResultSuccess; use Illuminate\Http\Response; abstract class Service @@ -39,9 +40,9 @@ abstract class Service return $this->error(Response::HTTP_UNAUTHORIZED, $message); } - final protected function ok(string $message = 'OK'): ServiceResultArray + final protected function ok(string $message = 'OK'): ServiceResultSuccess { - return $this->result(['message' => $message]); + return new ServiceResultSuccess($message); } final protected function result(array $data = []): ServiceResultArray