File: D:/HostingSpaces/SBogers10/blije-gasten.komma.pro/app/Komma/Kms/ActionLog/ActionLogModelService.php
<?php
namespace App\Komma\Kms\ActionLog;
use App\Komma\Kms\ActionLog\Models\ActionLog;
use App\Komma\Kms\Core\HouseKeeping\CanDoHousekeepingInterface;
use App\Komma\Kms\Core\ModelService;
use App\Komma\Users\Models\KmsUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
/**
* Class ImportExportService
*
* Directs Import and Export services to do their job.
* And is responsible for loading and offering files for download
*
* @package App\Komma\Kms\Transfer
*/
final class ActionLogModelService extends ModelService implements CanDoHousekeepingInterface
{
public function __construct()
{
$this->setModelClassName(ActionLog::class);
parent::__construct();
}
/**
* Log an action to the database
*
*
* @param string $actionText
* @param null $payLoad Example: "['route' => '/login']". Don't make it to big!
* @param Authenticatable $user The user or null for the authenticated user / anonymous
* @return ActionLog
* @throws \Exception
*/
public static function Log(string $actionText, $payLoad = null, Authenticatable $user = null):ActionLog
{
$action = new ActionLog();
if($user && $user->exists) $action->authenticatable()->associate($user);
$action->action = $actionText;
if($payLoad) $action->payload = $payLoad;
$action->save();
return $action;
}
/**
* Returns the logs between a date range
*
* @param null|string|Carbon $from
* @param null|string|Carbon $trough
*
* @param int $authenticatableFilterId
* @param string|null $authenticatableFilterClass
* @return Builder
*/
public static function getLogs($from = null, $trough = null, int $authenticatableFilterId = 0, string $authenticatableFilterClass = null)
{
if(!is_string($from) && !is_a($from, Carbon::class)) throw new \RuntimeException('from must be either a time string or a '.Carbon::class.' instance');
if(!is_string($trough) && !is_a($trough, Carbon::class)) throw new \RuntimeException('trough must be either a time string or a '.Carbon::class.' instance');
if(!is_a($from, Carbon::class)) $from = Carbon::createFromFormat('Y-m-d H:i:s', $from, 'Europe/Amsterdam');
if(!is_a($trough, Carbon::class)) $trough = Carbon::createFromFormat('Y-m-d H:i:s', $trough, 'Europe/Amsterdam')->setTime(23,59,59);
$logsQueryBuilder = ActionLog::with('authenticatable')
->where([
['created_at', '>=', $from],
['created_at', '<=', $trough]
])->orderByDesc('created_at');
if($authenticatableFilterId !== 0 && $authenticatableFilterClass) {
$logsQueryBuilder->where('authenticatable_id', '=', $authenticatableFilterId)->where('authenticatable_type', '=', KmsUser::class);
}
return $logsQueryBuilder;
}
/**
* Delete logs older than 2 years and the oldest when there are more then 100000
*/
public static function doHouseKeeping()
{
$result = [];
$newerLogsDate = Carbon::now()->subYear(2);
$newerLogsDate = Carbon::createFromFormat('Y-m-d H:i:s', $newerLogsDate, 'Europe/Amsterdam');
$expiredLogsToDelete = ActionLog::where('created_at', '<=', $newerLogsDate)->get(['id']);
$ids = $expiredLogsToDelete->pluck('id');
$expiredLogsCount = $ids->count();
if($expiredLogsCount > 0) {
ActionLog::destroy($ids->toArray());
$result[] = 'Deleted ' . $expiredLogsCount . ' logs which where older then 2 years ago';
}
$count = ActionLog::count();
$skip = (ActionLog::count() < 10000) ? ActionLog::count() : 10000;
$limit = $count - $skip;
$oldestLogsThatExceedQuantity = ActionLog::orderBy('created_at','DESC')->skip($skip)->take($limit)->get(['id']);
$ids = $oldestLogsThatExceedQuantity->pluck(['id']);
$exceededQuantityCount = $ids->count();
if($exceededQuantityCount > 0) {
ActionLog::destroy($ids->toArray());
$result[] = 'Deleted ' . $exceededQuantityCount . ' oldest logs which exceeded the quantity limit of ' . $skip;
}
return implode(PHP_EOL, $result);
}
}