Skip to content

Commit

Permalink
[2.x] - #5 System Log DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Nandovga committed Sep 26, 2024
1 parent 16d4545 commit d2a3e60
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Middleware/LogDB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Orangesix\Middleware;

use Closure;
use Illuminate\Http\Request;
use Orangesix\Service\Log\LogService;

class LogDB
{
/**
* Realiza gestão do Log via banco de dados
* @param Request $request
* @param Closure $next
* @return mixed
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function handle(Request $request, Closure $next): mixed
{
$ignore = config('logging.db_route_ignore');
(app()->make(LogService::class))->dispatch(empty($ignore) ? [] : $ignore);
return $next($request);
}
}
21 changes: 21 additions & 0 deletions src/Models/Log/LogModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Orangesix\Models\Log;

use Illuminate\Database\Eloquent\Model;

class LogModel extends Model
{
/** @var string */
protected $table = 'log';

/** @var string */
protected $connection = 'mysql';

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = env('LOG_DB', 'log');
$this->connection = env('DB_CONNECTION', 'mysql');
}
}
23 changes: 23 additions & 0 deletions src/Repository/Log/LogRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Orangesix\Repository\Log;

use Orangesix\Models\Log\LogModel;
use Orangesix\Repository\Repository;
use Orangesix\Repository\RepositoryDataBase;

/**
* Repository - Log
*/
class LogRepository implements Repository
{
use RepositoryDataBase;

/** @var LogModel */
private LogModel $model;

public function __construct(LogModel $model)
{
$this->model = $model;
}
}
89 changes: 89 additions & 0 deletions src/Service/Log/LogService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Orangesix\Service\Log;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Orangesix\Service\ServiceBase;
use Orangesix\Repository\Log\LogRepository;

/**
* Repository - LOG
*/
class LogService extends ServiceBase
{
public function __construct(LogRepository $repository)
{
parent::__construct($repository);
}

/**
* Realiza o disparo do LOG
* @return void
*/
public function dispatch(array $ignore = []): void
{
try {
DB::connection(env('DB_CONNECTION', 'mysql'))->getPdo();

if (Schema::hasTable(env('LOG_DB', 'log'))) {
$this->log($ignore);
}
} catch (\Exception $exception) {
return;
}
}

/**
* Realiza a validação dos dados LOG
* @param Request $request
* @return array
*/
public function validated(Request $request): array
{
$data = $request->validate([
'log_id_usuario' => '',
'log_usuario' => '',
'log_rota' => 'required',
'log_ip' => 'required',
'log_action' => 'required',
'log_dados' => '',
]);

$now = date('Y-m-d H:i');
if ($this->repository->getModel()::query()
->where('log_id_usuario', '=', $data['log_id_usuario'])
->where('log_usuario', '=', $data['log_usuario'])
->where('log_rota', '=', $data['log_rota'])
->where('log_dados', '=', $data['log_dados'])
->whereRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') = '{$now}'")
->exists()) {
throw new \Exception('Log já existe');
}

return $data;
}

/**
* Realiza a gestão do LOG
* @return void
*/
private function log(array $ignore = []): void
{
$user = Auth::user();
$dados = \request()->all();
$rota = \request()->route()->getName();
if (!in_array($rota, $ignore)) {
$this->manager(new Request([
'log_id_usuario' => $user?->id,
'log_usuario' => $user?->nome,
'log_rota' => \request()->route()->getName(),
'log_ip' => \request()->ip(),
'log_action' => \request()->method(),
'log_dados' => empty($dados) || (isset($dados['iframe']) && $dados['iframe'] == '1') ? null : json_encode($dados)
]));
}
}
}

0 comments on commit d2a3e60

Please sign in to comment.