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/hours.komma.pro/app/Console/Commands/cleanActivities.php
<?php

namespace App\Console\Commands;

use App\Komma\ActivityLog\Activity;
use App\Komma\Commands\CommandsService;
use Illuminate\Console\Command;
use Carbon\Carbon;

class cleanActivities extends Command
{
    protected $cleanupService;


    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cleanup:activities';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Clean the activities of more then one year ago';


    public function __construct(CommandsService $cleanupService)
    {
        parent::__construct();
        $this->cleanupService = $cleanupService;
    }

    /**
     * Execute the command.
     *
     * @return void
     */
    public function handle()
    {
        //make counter array
        $counterLog = [];
        //create expire date
        $expireDate = Carbon::today()->subYear()->toDateTimeString();

        //laravel log
        \Log::info('start cleanup');

        $counterLog = \DB::transaction(function () use ($expireDate, $counterLog) {

            //get expired rows
            $activities = Activity::where('created_at', '<', $expireDate)->get();

            //delete the expired rows
            $this->cleanupService->cleanupActivities($activities);

            //set counter
            return $counterLog += ['activities' => count($activities)];

        });
        
        //laravel log
        \Log::info('end cleanup');
        \Log::info($counterLog);
    }
}