2023-06-28 17:29:56 +06:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\ServiceResults\ServiceResultArray;
|
|
|
|
use App\ServiceResults\ServiceResultError;
|
2023-07-06 21:54:10 +06:00
|
|
|
use App\ServiceResults\ServiceResultSuccess;
|
2023-07-16 19:21:09 +06:00
|
|
|
use App\ServiceResults\StoreUpdateResult;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-07-06 10:48:32 +06:00
|
|
|
use Illuminate\Http\Response;
|
2023-06-28 17:29:56 +06:00
|
|
|
|
|
|
|
abstract class Service
|
|
|
|
{
|
|
|
|
final protected function errValidate(string $message, array $errors = []): ServiceResultError
|
|
|
|
{
|
2023-07-06 10:48:32 +06:00
|
|
|
return $this->error(Response::HTTP_UNPROCESSABLE_ENTITY, $message, $errors);
|
2023-06-28 17:29:56 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
final protected function errFobidden(string $message): ServiceResultError
|
|
|
|
{
|
2023-07-06 10:48:32 +06:00
|
|
|
return $this->error(Response::HTTP_FORBIDDEN, $message);
|
2023-06-28 17:29:56 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
final protected function errNotFound(string $message): ServiceResultError
|
|
|
|
{
|
2023-07-06 10:48:32 +06:00
|
|
|
return $this->error(Response::HTTP_NOT_FOUND, $message);
|
2023-06-28 17:29:56 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
final protected function errService(string $message): ServiceResultError
|
|
|
|
{
|
2023-07-06 10:48:32 +06:00
|
|
|
return $this->error(Response::HTTP_INTERNAL_SERVER_ERROR, $message);
|
2023-06-28 17:29:56 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
final protected function notAcceptable(string $message): ServiceResultError
|
|
|
|
{
|
2023-07-06 10:48:32 +06:00
|
|
|
return $this->error(Response::HTTP_NOT_ACCEPTABLE, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
final protected function errUnauthorized(string $message): ServiceResultError
|
|
|
|
{
|
|
|
|
return $this->error(Response::HTTP_UNAUTHORIZED, $message);
|
2023-06-28 17:29:56 +06:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:54:10 +06:00
|
|
|
final protected function ok(string $message = 'OK'): ServiceResultSuccess
|
2023-06-28 17:29:56 +06:00
|
|
|
{
|
2023-07-06 21:54:10 +06:00
|
|
|
return new ServiceResultSuccess($message);
|
2023-06-28 17:29:56 +06:00
|
|
|
}
|
|
|
|
|
2023-07-16 19:21:09 +06:00
|
|
|
final protected function resultStoreUpdateModel(Model $model, string $message = 'OK'): StoreUpdateResult
|
|
|
|
{
|
|
|
|
return new StoreUpdateResult($model, $message);
|
|
|
|
}
|
|
|
|
|
2023-06-28 17:29:56 +06:00
|
|
|
final protected function result(array $data = []): ServiceResultArray
|
|
|
|
{
|
|
|
|
return new ServiceResultArray(data: $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
final protected function error(int $code, string $message, array $errors = []): ServiceResultError
|
|
|
|
{
|
|
|
|
return new ServiceResultError(
|
|
|
|
message: $message,
|
|
|
|
errors: $errors,
|
|
|
|
code: $code
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|