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.

This commit is contained in:
Leonid Nikitin 2023-07-06 21:54:10 +06:00
parent c18e7e54b7
commit 91810190b7
Signed by: kor-elf
GPG Key ID: 7DE8F80C5CEC2C0D
3 changed files with 20 additions and 4 deletions

View File

@ -0,0 +1,15 @@
<?php declare(strict_types=1);
namespace App\ServiceResults;
final class ServiceResultSuccess extends ServiceResult
{
public function __construct(
private readonly string $message
) { }
public function getMessage(): string
{
return $this->message;
}
}

View File

@ -4,8 +4,8 @@ namespace App\Services;
use App\Dto\Request\Authorization; use App\Dto\Request\Authorization;
use App\Repositories\UserRepository; use App\Repositories\UserRepository;
use App\ServiceResults\ServiceResultArray;
use App\ServiceResults\ServiceResultError; use App\ServiceResults\ServiceResultError;
use App\ServiceResults\ServiceResultSuccess;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
@ -15,7 +15,7 @@ final class AuthService extends Service
private readonly UserRepository $userRepository private readonly UserRepository $userRepository
) { } ) { }
public function authorization(Authorization $authorization): ServiceResultError | ServiceResultArray public function authorization(Authorization $authorization): ServiceResultError | ServiceResultSuccess
{ {
$user = $this->userRepository->getUserByEmail($authorization->getEmail()); $user = $this->userRepository->getUserByEmail($authorization->getEmail());
if (is_null($user)) { if (is_null($user)) {

View File

@ -5,6 +5,7 @@ namespace App\Services;
use App\ServiceResults\ServiceResultArray; use App\ServiceResults\ServiceResultArray;
use App\ServiceResults\ServiceResultError; use App\ServiceResults\ServiceResultError;
use App\ServiceResults\ServiceResultSuccess;
use Illuminate\Http\Response; use Illuminate\Http\Response;
abstract class Service abstract class Service
@ -39,9 +40,9 @@ abstract class Service
return $this->error(Response::HTTP_UNAUTHORIZED, $message); 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 final protected function result(array $data = []): ServiceResultArray