2016-06-10 14:00:00 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ImportJob.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-06-10 14:00:00 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05: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 07:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-06-10 14:00:00 -05:00
|
|
|
*/
|
2017-03-24 09:15:12 -05:00
|
|
|
declare(strict_types=1);
|
2016-06-10 14:00:00 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Models;
|
|
|
|
|
2016-06-27 08:15:46 -05:00
|
|
|
use Crypt;
|
2017-12-23 10:42:07 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-06-10 14:00:00 -05:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-11-20 04:40:05 -06:00
|
|
|
use Log;
|
2016-06-27 08:15:46 -05:00
|
|
|
use Storage;
|
2016-06-10 14:00:00 -05:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
|
2016-11-18 13:06:08 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class ImportJob.
|
2016-11-18 13:06:08 -06:00
|
|
|
*/
|
2016-06-10 14:00:00 -05:00
|
|
|
class ImportJob extends Model
|
|
|
|
{
|
2016-12-24 10:36:51 -06:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-12-23 10:42:07 -06:00
|
|
|
public $validStatus
|
2016-12-24 10:36:51 -06:00
|
|
|
= [
|
2017-12-23 10:42:07 -06:00
|
|
|
'new',
|
|
|
|
'configuring',
|
|
|
|
'configured',
|
|
|
|
'running',
|
|
|
|
'error',
|
|
|
|
'finished',
|
2016-12-24 10:36:51 -06:00
|
|
|
];
|
2017-12-17 07:30:53 -06:00
|
|
|
/**
|
2017-12-23 10:42:07 -06:00
|
|
|
* The attributes that should be casted to native types.
|
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @var array
|
|
|
|
*/
|
2017-12-23 10:42:07 -06:00
|
|
|
protected $casts
|
2016-08-24 12:33:58 -05:00
|
|
|
= [
|
2017-12-23 10:42:07 -06:00
|
|
|
'created_at' => 'datetime',
|
|
|
|
'updated_at' => 'datetime',
|
2016-08-24 12:33:58 -05:00
|
|
|
];
|
|
|
|
|
2016-06-10 14:00:00 -05:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return mixed
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-06-10 14:00:00 -05:00
|
|
|
* @throws NotFoundHttpException
|
2017-12-23 10:42:07 -06:00
|
|
|
* @throws FireflyException
|
2016-06-10 14:00:00 -05:00
|
|
|
*/
|
2018-02-09 12:24:15 -06:00
|
|
|
public static function routeBinder(string $value): ImportJob
|
2016-06-10 14:00:00 -05:00
|
|
|
{
|
2018-02-09 12:28:16 -06:00
|
|
|
if (auth()->check()) {
|
2017-12-25 01:45:23 -06:00
|
|
|
$key = trim($value);
|
2018-02-09 12:28:16 -06:00
|
|
|
$importJob = auth()->user()->importJobs()->where('key', $key)->first();
|
2017-12-25 01:45:23 -06:00
|
|
|
if (null !== $importJob) {
|
2017-12-23 10:42:07 -06:00
|
|
|
// must have valid status:
|
2017-12-25 01:45:23 -06:00
|
|
|
if (!in_array($importJob->status, $importJob->validStatus)) {
|
|
|
|
throw new FireflyException(sprintf('ImportJob with key "%s" has invalid status "%s"', $importJob->key, $importJob->status));
|
2017-12-23 10:42:07 -06:00
|
|
|
}
|
2017-12-25 01:45:23 -06:00
|
|
|
|
|
|
|
return $importJob;
|
2016-06-10 14:00:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
}
|
|
|
|
|
2016-08-13 14:51:01 -05:00
|
|
|
/**
|
|
|
|
* @param int $count
|
|
|
|
*/
|
|
|
|
public function addTotalSteps(int $count)
|
|
|
|
{
|
2017-03-24 09:15:12 -05:00
|
|
|
$status = $this->extended_status;
|
2017-06-23 22:49:33 -05:00
|
|
|
$status['steps'] += $count;
|
2016-08-13 14:51:01 -05:00
|
|
|
$this->extended_status = $status;
|
|
|
|
$this->save();
|
2018-01-08 13:20:45 -06:00
|
|
|
Log::debug(sprintf('Add %d to total steps for job "%s" making total steps %d', $count, $this->key, $status['steps']));
|
2016-08-13 14:51:01 -05:00
|
|
|
}
|
|
|
|
|
2016-06-10 14:00:00 -05:00
|
|
|
/**
|
2017-12-23 10:42:07 -06:00
|
|
|
* @param string $status
|
|
|
|
*
|
|
|
|
* @throws FireflyException
|
2016-06-10 14:00:00 -05:00
|
|
|
*/
|
2017-12-23 10:42:07 -06:00
|
|
|
public function change(string $status): void
|
2016-06-10 14:00:00 -05:00
|
|
|
{
|
2017-12-23 10:42:07 -06:00
|
|
|
if (in_array($status, $this->validStatus)) {
|
2017-12-28 11:38:59 -06:00
|
|
|
Log::debug(sprintf('Job status set (in model) to "%s"', $status));
|
2017-12-23 10:42:07 -06:00
|
|
|
$this->status = $status;
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new FireflyException(sprintf('Status "%s" is invalid for job "%s".', $status, $this->key));
|
|
|
|
|
2016-06-10 14:00:00 -05:00
|
|
|
}
|
|
|
|
|
2016-06-24 07:24:34 -05:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getConfigurationAttribute($value)
|
|
|
|
{
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $value) {
|
2016-09-24 10:22:42 -05:00
|
|
|
return [];
|
|
|
|
}
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === strlen($value)) {
|
2016-06-24 07:24:34 -05:00
|
|
|
return [];
|
|
|
|
}
|
2016-06-27 08:15:46 -05:00
|
|
|
|
|
|
|
return json_decode($value, true);
|
2016-06-24 07:24:34 -05:00
|
|
|
}
|
|
|
|
|
2016-08-13 14:51:01 -05:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getExtendedStatusAttribute($value)
|
|
|
|
{
|
2018-01-05 10:29:42 -06:00
|
|
|
if (0 === strlen(strval($value))) {
|
2016-08-13 14:51:01 -05:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_decode($value, true);
|
|
|
|
}
|
|
|
|
|
2016-06-24 07:24:34 -05:00
|
|
|
/**
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-12-28 11:38:59 -06:00
|
|
|
*
|
2016-06-24 07:24:34 -05:00
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setConfigurationAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['configuration'] = json_encode($value);
|
|
|
|
}
|
2016-06-27 08:15:46 -05:00
|
|
|
|
2016-08-13 14:51:01 -05:00
|
|
|
/**
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-12-28 11:38:59 -06:00
|
|
|
*
|
2016-08-13 14:51:01 -05:00
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setExtendedStatusAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['extended_status'] = json_encode($value);
|
|
|
|
}
|
|
|
|
|
2016-08-24 12:33:58 -05:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setStatusAttribute(string $value)
|
|
|
|
{
|
|
|
|
if (in_array($value, $this->validStatus)) {
|
|
|
|
$this->attributes['status'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 08:15:46 -05:00
|
|
|
/**
|
|
|
|
* @return string
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
2016-06-27 08:15:46 -05:00
|
|
|
*/
|
|
|
|
public function uploadFileContents(): string
|
|
|
|
{
|
|
|
|
$fileName = $this->key . '.upload';
|
|
|
|
$disk = Storage::disk('upload');
|
|
|
|
$encryptedContent = $disk->get($fileName);
|
|
|
|
$content = Crypt::decrypt($encryptedContent);
|
2018-01-06 12:17:47 -06:00
|
|
|
$content = trim($content);
|
2017-06-23 22:49:33 -05:00
|
|
|
Log::debug(sprintf('Content size is %d bytes.', strlen($content)));
|
2016-06-27 08:15:46 -05:00
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2016-06-27 08:15:46 -05:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('FireflyIII\User');
|
|
|
|
}
|
2016-06-10 14:00:00 -05:00
|
|
|
}
|