Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
Instanciar de forma automática os services dentro do controller, service e repository
  • Loading branch information
Nandovga committed Jan 29, 2024
1 parent a7a9afd commit 7800263
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Controller/ControllerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class ControllerBase extends Controller
protected ServiceResponse $response;

/** @var array */
private array $autoInstance = [];
private ?array $autoInstance;

/**
* @throws BindingResolutionException
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
8 changes: 7 additions & 1 deletion src/Repository/RepositoryDataBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
80 changes: 80 additions & 0 deletions src/Repository/RepositoryTransferList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Orangecode\Repository;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

trait RepositoryTransferList
{
/** @var Model */
private Model $transferList;

/** @var array */
private array $keys;

/**
* Realiza a parametrização do transferList
*/
public function transferList(string $model, array $keys = []): Repository
{
$this->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();
}
}
11 changes: 6 additions & 5 deletions src/Service/ServiceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class ServiceBase implements Service
protected ServiceResponse $response;

/** @var array */
private array $autoInstance = [];
private ?array $autoInstance;

/**
* @param Repository $repository
Expand All @@ -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);
}
Expand Down

0 comments on commit 7800263

Please sign in to comment.