1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Console/Commands/RemoveOrphanedDocuments.php

62 lines
1.3 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Console\Commands;
use App\Models\Document;
2017-01-30 20:40:43 +01:00
use DateTime;
use Illuminate\Console\Command;
2017-05-01 14:46:57 +02:00
use Symfony\Component\Console\Input\InputOption;
/**
2017-01-30 20:40:43 +01:00
* Class RemoveOrphanedDocuments.
*/
class RemoveOrphanedDocuments extends Command
{
/**
* @var string
*/
protected $name = 'ninja:remove-orphaned-documents';
/**
* @var string
*/
protected $description = 'Removes old documents not associated with an expense or invoice';
2017-05-01 14:17:52 +02:00
public function fire()
{
2017-10-24 09:59:26 +02:00
$this->info(date('r').' Running RemoveOrphanedDocuments...');
2017-05-01 14:17:52 +02:00
if ($database = $this->option('database')) {
config(['database.default' => $database]);
}
$documents = Document::whereRaw('invoice_id IS NULL AND expense_id IS NULL AND updated_at <= ?', [new DateTime('-1 hour')])
->get();
2017-05-01 14:17:52 +02:00
$this->info($documents->count() . ' orphaned document(s) found');
foreach ($documents as $document) {
$document->delete();
}
$this->info('Done');
}
/**
* @return array
*/
protected function getArguments()
{
return [];
}
/**
* @return array
*/
protected function getOptions()
{
2017-05-01 14:17:52 +02:00
return [
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
];
}
}