HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/Kms/ActionLog/ActionLogService.php
<?php


namespace App\KommaApp\Kms\ActionLog;


use App\KommaApp\Kms\Core\HouseKeeping\CanDoHousekeepingInterface;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\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
 *
 * @package App\KommaApp\Kms\Transfer
 */
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
     *
     * @return ActionLog
     */
    public static function getLogs($from = null, $trough = 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);

        $logs = ActionLog::with('user')
        ->where([
            ['created_at', '>=', $from],
            ['created_at', '<=', $trough]
        ]);

        return $logs;
    }

    /**
     * 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  = 100000;
        $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);
    }
}