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