From 780026344da456b6f31125257797e40bc195567e Mon Sep 17 00:00:00 2001 From: Luiz Fernando Bernardes de Paula Date: Mon, 29 Jan 2024 16:54:19 -0300 Subject: [PATCH] 1.0.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instanciar de forma automática os services dentro do controller, service e repository --- src/Controller/ControllerBase.php | 2 +- src/Exceptions/Message.php | 2 +- src/Repository/RepositoryDataBase.php | 8 ++- src/Repository/RepositoryTransferList.php | 80 +++++++++++++++++++++++ src/Service/ServiceBase.php | 11 ++-- 5 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/Repository/RepositoryTransferList.php diff --git a/src/Controller/ControllerBase.php b/src/Controller/ControllerBase.php index 4589f5f..5c32ef1 100644 --- a/src/Controller/ControllerBase.php +++ b/src/Controller/ControllerBase.php @@ -14,7 +14,7 @@ abstract class ControllerBase extends Controller protected ServiceResponse $response; /** @var array */ - private array $autoInstance = []; + private ?array $autoInstance; /** * @throws BindingResolutionException diff --git a/src/Exceptions/Message.php b/src/Exceptions/Message.php index bd378c0..4ab1779 100644 --- a/src/Exceptions/Message.php +++ b/src/Exceptions/Message.php @@ -17,7 +17,7 @@ public function render(): JsonResponse default => MessageEnum::Error, }; return response()->json([ - 'message' => new \Orangecode\Helpers\Service\Response\Message($this->getMessage(), $type) + 'message' => new \Orangecode\Service\Response\Message($this->getMessage(), $type) ], $this->getCode()); } } diff --git a/src/Repository/RepositoryDataBase.php b/src/Repository/RepositoryDataBase.php index 7400c35..1be1175 100644 --- a/src/Repository/RepositoryDataBase.php +++ b/src/Repository/RepositoryDataBase.php @@ -5,13 +5,16 @@ use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Database\Eloquent\Model; use Orangecode\Models\ModelAutoInstance; +use Orangecode\Service\ServiceAutoInstance; trait RepositoryDataBase { + use RepositoryTransferList; use ModelAutoInstance; + use ServiceAutoInstance; /** @var array */ - private array $autoInstance = []; + private ?array $autoInstance; /** * @param string $name @@ -20,6 +23,9 @@ trait RepositoryDataBase */ public function __get(string $name) { + if (strpos($name, 'service') !== false) { + return $this->instanceAutoService($name, $this->autoInstance['service'] ?? null); + } return $this->instanceAutoModel($name, $this->autoInstance['model'] ?? null); } diff --git a/src/Repository/RepositoryTransferList.php b/src/Repository/RepositoryTransferList.php new file mode 100644 index 0000000..60355a6 --- /dev/null +++ b/src/Repository/RepositoryTransferList.php @@ -0,0 +1,80 @@ +transferList = app()->make($model); + $this->keys = $keys; + return $this; + } + + /** + * Realiza o gerenciamento do transferList + */ + public function manager(Request $request): void + { + try { + DB::beginTransaction(); + if (empty($request->list) || !isset($this->keys[0]) || !isset($this->keys[1])) { + abort(500, 'Não foi possível atualizar a lista de dados.'); + } + foreach ($request->list as $list) { + if (isset($list['active']) && $list['active']) { + $this->active([ + $this->keys[0] => $request[$this->keys[0]], + $this->keys[1] => $list[$this->keys[1]], + ]); + } else { + $this->disabled([ + $this->keys[0] => $request[$this->keys[0]], + $this->keys[1] => $list[$this->keys[1]], + ]); + } + } + DB::commit(); + } catch (\Exception $exception) { + DB::rollBack(); + abort(500, $exception->getMessage()); + } + } + + /** + * Realiza a ativação do transfer list + */ + private function active(array $data): void + { + $this->transferList::updateOrCreate( + [ + $this->keys[0] => $data[$this->keys[0]], + $this->keys[1] => $data[$this->keys[1]] + ], + $data + )?->id; + } + + /** + * Realiza a desativação do transfer list + */ + private function disabled(array $data): void + { + $this->transferList::where($this->keys[0], $data[$this->keys[0]]) + ->where($this->keys[1], '=', $data[$this->keys[1]]) + ->delete(); + } +} diff --git a/src/Service/ServiceBase.php b/src/Service/ServiceBase.php index 4d54408..2a5fb7d 100644 --- a/src/Service/ServiceBase.php +++ b/src/Service/ServiceBase.php @@ -22,7 +22,7 @@ abstract class ServiceBase implements Service protected ServiceResponse $response; /** @var array */ - private array $autoInstance = []; + private ?array $autoInstance; /** * @param Repository $repository @@ -44,11 +44,12 @@ public function __construct(Repository $repository, ?array $autoInstance = null) public function __call(string $name, array $arguments) { if (method_exists($this->repository, $name)) { - if (empty($arguments)) { - return $this->repository->$name(); - } else { - return $this->repository->$name($arguments[0]); + $reflection = new \ReflectionMethod($this->repository, $name); + $args = $reflection->getNumberOfParameters(); + for ($i = 0; $i < $args; $i++) { + ${'arg_' . $i} = $arguments[$i] ?? null; } + return $this->repository->$name($arg_0 ?? null, $arg_1 ?? null, $arg_2 ?? null, $arg_3 ?? null, $arg_4 ?? null); } else { throw new \Exception('Método não existe no service ou repository.', 500); }