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

150 lines
3.6 KiB
PHP
Raw Normal View History

2016-02-04 17:16:16 +01:00
<?php
/**
* ExportJobRepository.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-02-04 17:16:16 +01:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-02-04 17:16:16 +01:00
*/
declare(strict_types=1);
2016-02-04 17:16:16 +01:00
namespace FireflyIII\Repositories\ExportJob;
use Carbon\Carbon;
2018-08-04 17:30:06 +02:00
use Exception;
2016-02-04 17:16:16 +01:00
use FireflyIII\Models\ExportJob;
2016-03-03 08:53:05 +01:00
use FireflyIII\User;
2018-07-26 06:10:17 +02:00
use Illuminate\Contracts\Filesystem\FileNotFoundException;
2016-02-04 17:16:16 +01:00
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;
use Log;
2016-02-04 17:16:16 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class ExportJobRepository.
2016-02-04 17:16:16 +01:00
*/
class ExportJobRepository implements ExportJobRepositoryInterface
{
2016-03-03 08:53:05 +01:00
/** @var User */
private $user;
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
}
2016-12-27 15:46:52 +01:00
/**
* @param ExportJob $job
* @param string $status
*
* @return bool
*/
public function changeStatus(ExportJob $job, string $status): bool
{
Log::debug(sprintf('Change status of job #%d to "%s"', $job->id, $status));
2018-08-06 19:14:30 +02:00
$job->status = $status;
$job->save();
2016-12-27 15:46:52 +01:00
return true;
}
2016-02-04 17:16:16 +01:00
/**
2018-07-25 06:45:25 +02:00
* @return ExportJob|null
2016-02-04 17:16:16 +01:00
*/
2018-07-25 06:45:25 +02:00
public function create(): ?ExportJob
2016-02-04 17:16:16 +01:00
{
2016-05-02 20:49:19 +02:00
$count = 0;
while ($count < 30) {
$key = Str::random(12);
$existing = $this->findByKey($key);
2018-07-24 20:30:52 +02:00
if (null === $existing) {
2016-05-02 20:49:19 +02:00
$exportJob = new ExportJob;
$exportJob->user()->associate($this->user);
$exportJob->key = Str::random(12);
$exportJob->status = 'export_status_never_started';
$exportJob->save();
2016-05-15 18:36:40 +02:00
2016-05-02 20:49:19 +02:00
// breaks the loop:
return $exportJob;
}
2017-11-15 12:25:49 +01:00
++$count;
2016-05-02 20:49:19 +02:00
}
2018-07-25 06:45:25 +02:00
return null;
2016-02-04 17:16:16 +01:00
}
2016-12-25 12:55:22 +01:00
/**
* @param ExportJob $job
*
* @return bool
*/
public function exists(ExportJob $job): bool
{
$disk = Storage::disk('export');
$file = $job->key . '.zip';
return $disk->exists($file);
}
2016-02-04 17:16:16 +01:00
/**
2016-02-05 09:25:15 +01:00
* @param string $key
2018-07-26 06:10:17 +02:00
*
2018-07-24 20:30:52 +02:00
* @return ExportJob|null
2016-02-04 17:16:16 +01:00
*/
2018-07-24 20:30:52 +02:00
public function findByKey(string $key): ?ExportJob
2016-02-04 17:16:16 +01:00
{
2018-07-22 18:50:27 +02:00
/** @var ExportJob $result */
$result = $this->user->exportJobs()->where('key', $key)->first(['export_jobs.*']);
2017-11-15 12:25:49 +01:00
if (null === $result) {
2018-07-24 20:30:52 +02:00
return null;
2016-05-02 20:49:19 +02:00
}
return $result;
2016-02-04 17:16:16 +01:00
}
2016-12-25 12:55:22 +01:00
/**
* @param ExportJob $job
*
* @return string
*/
public function getContent(ExportJob $job): string
{
2018-08-04 17:30:06 +02:00
$disk = Storage::disk('export');
$file = $job->key . '.zip';
2018-07-27 04:46:21 +02:00
2018-07-26 06:10:17 +02:00
try {
$content = $disk->get($file);
} catch (FileNotFoundException $e) {
Log::warning(sprintf('File not found: %s', $e->getMessage()));
2018-07-27 04:46:21 +02:00
$content = '';
2018-07-26 06:10:17 +02:00
}
2016-12-25 12:55:22 +01:00
2018-07-26 06:10:17 +02:00
return $content;
2016-12-25 12:55:22 +01:00
}
2017-01-30 16:46:30 +01:00
/**
* @param User $user
*/
2018-07-22 18:50:27 +02:00
public function setUser(User $user): void
2017-01-30 16:46:30 +01:00
{
$this->user = $user;
}
2016-02-10 16:01:18 +01:00
}