File: D:/HostingSpaces/MdnDirecteur/hours.komma.cloud/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);
}
}