Introduced ServiceResult and ServiceResultError interfaces, and respective classes with related methods. These changes were necessary to add and handle service results throughout the app. We now have a standard way to return and handle both successes and errors from our services. This leads to cleaner, clearer, and more maintainable code.
This commit is contained in:
parent
20fbdd7a34
commit
00177a134e
9
app/Contracts/ServiceResult.php
Normal file
9
app/Contracts/ServiceResult.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Contracts;
|
||||||
|
|
||||||
|
interface ServiceResult
|
||||||
|
{
|
||||||
|
public function isSuccess(): bool;
|
||||||
|
}
|
12
app/Contracts/ServiceResultError.php
Normal file
12
app/Contracts/ServiceResultError.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Contracts;
|
||||||
|
|
||||||
|
interface ServiceResultError
|
||||||
|
{
|
||||||
|
public function getCode(): ?int;
|
||||||
|
public function getMessage(): string;
|
||||||
|
public function getErrors(): array;
|
||||||
|
public function getData(): array;
|
||||||
|
}
|
15
app/ServiceResults/ServiceResult.php
Normal file
15
app/ServiceResults/ServiceResult.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\ServiceResults;
|
||||||
|
|
||||||
|
use App\Contracts\ServiceResult as ServiceResultContract;
|
||||||
|
use App\Contracts\ServiceResultError as ServiceResultErrorContract;
|
||||||
|
|
||||||
|
abstract class ServiceResult implements ServiceResultContract
|
||||||
|
{
|
||||||
|
public function isSuccess(): bool
|
||||||
|
{
|
||||||
|
return $this instanceof ServiceResultErrorContract !== true;
|
||||||
|
}
|
||||||
|
}
|
16
app/ServiceResults/ServiceResultArray.php
Normal file
16
app/ServiceResults/ServiceResultArray.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\ServiceResults;
|
||||||
|
|
||||||
|
final class ServiceResultArray extends ServiceResult
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly array $data,
|
||||||
|
) { }
|
||||||
|
|
||||||
|
public function getData(): array
|
||||||
|
{
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
}
|
40
app/ServiceResults/ServiceResultError.php
Normal file
40
app/ServiceResults/ServiceResultError.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\ServiceResults;
|
||||||
|
|
||||||
|
use App\Contracts\ServiceResultError as ServiceResultErrorContract;
|
||||||
|
|
||||||
|
final class ServiceResultError extends ServiceResult implements ServiceResultErrorContract
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly string $message,
|
||||||
|
private readonly array $errors = [],
|
||||||
|
private readonly ?int $code = null,
|
||||||
|
) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMessage(): string
|
||||||
|
{
|
||||||
|
return $this->message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCode(): ?int
|
||||||
|
{
|
||||||
|
return $this->code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getErrors(): array
|
||||||
|
{
|
||||||
|
return $this->errors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getData(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'message' => $this->getMessage(),
|
||||||
|
'errors' => $this->errors
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user