Files
firefly-iii/app/Repositories/ExportJob/ExportJobRepository.php

97 lines
2.3 KiB
PHP
Raw Normal View History

2016-02-04 17:16:16 +01:00
<?php
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2016-02-04 17:16:16 +01:00
/**
* ExportJobRepository.php
2016-04-01 16:44:46 +02:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-02-04 17:16:16 +01:00
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Repositories\ExportJob;
use Carbon\Carbon;
use FireflyIII\Models\ExportJob;
2016-03-03 08:53:05 +01:00
use FireflyIII\User;
2016-02-04 17:16:16 +01:00
use Illuminate\Support\Str;
/**
* Class ExportJobRepository
*
* @package FireflyIII\Repositories\ExportJob
*/
class ExportJobRepository implements ExportJobRepositoryInterface
{
2016-03-03 08:53:05 +01:00
/** @var User */
private $user;
/**
* BillRepository constructor.
*
* @param User $user
*/
public function __construct(User $user)
{
$this->user = $user;
}
2016-02-04 17:16:16 +01:00
/**
* @return bool
*/
2016-04-05 22:00:03 +02:00
public function cleanup(): bool
2016-02-04 17:16:16 +01:00
{
$dayAgo = Carbon::create()->subDay();
2016-02-04 17:50:35 +01:00
$set = ExportJob::where('created_at', '<', $dayAgo->format('Y-m-d H:i:s'))
->whereIn('status', ['never_started', 'export_status_finished', 'export_downloaded'])
->get();
// loop set:
/** @var ExportJob $entry */
foreach ($set as $entry) {
$key = $entry->key;
$len = strlen($key);
$files = scandir(storage_path('export'));
/** @var string $file */
foreach ($files as $file) {
if (substr($file, 0, $len) === $key) {
unlink(storage_path('export') . DIRECTORY_SEPARATOR . $file);
}
}
$entry->delete();
}
2016-02-04 17:16:16 +01:00
return true;
}
/**
* @return ExportJob
*/
2016-04-05 22:00:03 +02:00
public function create(): ExportJob
2016-02-04 17:16:16 +01:00
{
$exportJob = new ExportJob;
2016-03-03 08:53:05 +01:00
$exportJob->user()->associate($this->user);
2016-02-04 17:16:16 +01:00
/*
* In theory this random string could give db error.
*/
$exportJob->key = Str::random(12);
$exportJob->status = 'export_status_never_started';
$exportJob->save();
return $exportJob;
}
/**
2016-04-05 22:00:03 +02:00
*
* FIXME this may return null
2016-04-25 18:43:09 +02:00
*
2016-02-05 09:25:15 +01:00
* @param string $key
2016-02-04 17:16:16 +01:00
*
* @return ExportJob|null
*/
2016-04-05 22:00:03 +02:00
public function findByKey(string $key): ExportJob
2016-02-04 17:16:16 +01:00
{
2016-03-03 08:53:05 +01:00
return $this->user->exportJobs()->where('key', $key)->first();
2016-02-04 17:16:16 +01:00
}
2016-02-10 16:01:18 +01:00
}