2016-06-10 14:00:00 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ImportJobRepository.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-06-10 14:00:00 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\ImportJob;
|
|
|
|
|
2016-09-17 02:50:40 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-06-10 14:00:00 -05:00
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ImportJobRepository
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Repositories\ImportJob
|
|
|
|
*/
|
|
|
|
class ImportJobRepository implements ImportJobRepositoryInterface
|
|
|
|
{
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ExportJobRepository constructor.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function __construct(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2017-01-30 09:42:58 -06:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2016-06-10 14:00:00 -05:00
|
|
|
/**
|
|
|
|
* @param string $fileType
|
|
|
|
*
|
|
|
|
* @return ImportJob
|
2016-10-09 00:58:27 -05:00
|
|
|
* @throws FireflyException
|
2016-06-10 14:00:00 -05:00
|
|
|
*/
|
|
|
|
public function create(string $fileType): ImportJob
|
|
|
|
{
|
2016-09-17 02:50:40 -05:00
|
|
|
$count = 0;
|
|
|
|
$fileType = strtolower($fileType);
|
|
|
|
$keys = array_keys(config('firefly.import_formats'));
|
|
|
|
if (!in_array($fileType, $keys)) {
|
|
|
|
throw new FireflyException(sprintf('Cannot use type "%s" for import job.', $fileType));
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:00:00 -05:00
|
|
|
while ($count < 30) {
|
|
|
|
$key = Str::random(12);
|
|
|
|
$existing = $this->findByKey($key);
|
|
|
|
if (is_null($existing->id)) {
|
|
|
|
$importJob = new ImportJob;
|
|
|
|
$importJob->user()->associate($this->user);
|
2016-08-13 14:51:01 -05:00
|
|
|
$importJob->file_type = $fileType;
|
|
|
|
$importJob->key = Str::random(12);
|
|
|
|
$importJob->status = 'import_status_never_started';
|
2016-08-14 04:31:09 -05:00
|
|
|
$importJob->extended_status = [
|
|
|
|
'total_steps' => 0,
|
|
|
|
'steps_done' => 0,
|
|
|
|
'import_count' => 0,
|
|
|
|
'importTag' => 0,
|
|
|
|
'errors' => [],
|
|
|
|
];
|
2016-06-10 14:00:00 -05:00
|
|
|
$importJob->save();
|
|
|
|
|
|
|
|
// breaks the loop:
|
|
|
|
return $importJob;
|
|
|
|
}
|
|
|
|
$count++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ImportJob;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return ImportJob
|
|
|
|
*/
|
|
|
|
public function findByKey(string $key): ImportJob
|
|
|
|
{
|
2016-08-11 03:21:32 -05:00
|
|
|
$result = $this->user->importJobs()->where('key', $key)->first(['import_jobs.*']);
|
2016-06-10 14:00:00 -05:00
|
|
|
if (is_null($result)) {
|
|
|
|
return new ImportJob;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2016-08-12 08:10:03 -05:00
|
|
|
}
|