File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Kms/ActionLog/ActionLogService.php
<?php
namespace App\Komma\Kms\ActionLog;
use App\Komma\Kms\Core\HouseKeeping\CanDoHousekeepingInterface;
use App\Komma\Kms\Core\Sections\SectionService;
use App\Komma\Users\Models\User;
use Carbon\Carbon;
/**
* Class ImportExportService
*
* Directs Import and Export services to do their job.
* And is responsible for loading and offering files for download
*/
class ActionLogService extends SectionService implements CanDoHousekeepingInterface
{
public function __construct()
{
$this->forModelName = ActionLog::class;
}
/**
* Log an action to the database
*
*
* @param string $actionText
* @param null $payLoad Example: "['route' => '/login']". Don't make it to big!
* @param User|null $user The user or null for the authenticated user / anonymous
* @return ActionLog
*/
public static function Log(string $actionText, $payLoad = null, User $user = null):ActionLog
{
if (! $user) {
if (\Auth::user()) {
$user = \Auth::user();
}
}
$action = new ActionLog();
$action->User()->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 $userFilter
* @return ActionLog
*/
public static function getLogs($from = null, $trough = null, int $userFilter = 0)
{
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('user')
->where([
['created_at', '>=', $from],
['created_at', '<=', $trough],
])->orderByDesc('created_at');
if ($userFilter !== 0) {
$logsQueryBuilder->where('user_id', '=', $userFilter);
}
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);
}
}