File: D:/HostingSpaces/SBogers10/hours.komma.pro/app/Console/Commands/cleanSoftDeletes.php
<?php
namespace App\Console\Commands;
use App\Komma\Absences\Absence;
use App\Komma\Cleanup\CleanupService;
use App\Komma\Commands\CommandsService;
use App\Komma\Companies\Company;
use App\Komma\Expenses\Expense;
use App\Komma\Hours\Hour;
use App\Komma\Projects\Project;
use App\Komma\Settings\AbsenceBalances\AbsenceBalance;
use App\Komma\Settings\Countries\Country;
use App\Komma\Settings\ExpenseTypes\ExpenseType;
use App\Komma\Settings\ExpenseUnits\ExpenseUnit;
use App\Komma\Settings\ProjectTemplates\ProjectTemplate;
use App\Komma\Settings\Subprojecttemplates\SubprojectTemplate;
use App\Komma\Settings\TaskTemplates\TaskTemplate;
use App\Komma\Subprojects\Subproject;
use App\Komma\Tasks\Task;
use Illuminate\Console\Command;
use Carbon\Carbon;
class cleanSoftDeletes extends Command
{
protected $cleanupService;
private $values = null;
protected $models = [
Hour::class,
Expense::class,
ExpenseType::class,
Absence::class,
AbsenceBalance::class,
Country::class,
ExpenseUnit::class,
ProjectTemplate::class,
SubprojectTemplate::class,
TaskTemplate::class,
Company::class,
Project::class,
Subproject::class,
Task::class
];
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cleanup:all';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clean all the tables from soft deleted rows';
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()->subMonth()->toDateTimeString();
//laravel log
\Log::info('start cleanup');
$counterLog = \DB::transaction(function () use ($expireDate, $counterLog) {
//loop trough array of tables
foreach ($this->tables as $model) {
//get expired rows
$model = new $model;
$this->values = $model->withTrashed()->where('deleted_at', '<', $expireDate)->get();
if($model == Company::class) $this->cleanupService->deleteCompanies($this->values);
if($model == Project::class) $this->cleanupService->deleteProjects($this->values);
if($model == Subproject::class) $this->cleanupService->deleteSubprojects($this->values);
if($model == Task::class) $this->cleanupService->deleteTasks($this->values);
//delete the expired rows
$this->cleanupService->cleanupDelete($this->values, $model);
//set counter
$counterLog += [$model => count($this->values)];
}
return $counterLog;
});
//laravel log
\Log::info('end cleanup');
\Log::info($counterLog);
}
}