firefly-iii/app/Models/ImportJob.php

181 lines
3.9 KiB
PHP
Raw Normal View History

2016-06-10 14:00:00 -05:00
<?php
/**
* ImportJob.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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\Models;
2016-06-27 08:15:46 -05:00
use Crypt;
2016-06-10 14:00:00 -05:00
use Illuminate\Database\Eloquent\Model;
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
/**
* Class ImportJob
*
* @package FireflyIII\Models
*/
2016-06-10 14:00:00 -05:00
class ImportJob extends Model
{
2016-12-24 10:36:51 -06:00
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts
= [
'created_at' => 'date',
'updated_at' => 'date',
];
/** @var array */
protected $dates = ['created_at', 'updated_at'];
protected $validStatus
= [
'import_status_never_started', // initial state
'import_configuration_saved', // import configuration saved. This step is going to be obsolete.
'settings_complete', // aka: ready for import.
'import_running', // import currently underway
'import_complete', // done with everything
];
2016-06-10 14:00:00 -05:00
/**
* @param $value
*
* @return mixed
* @throws NotFoundHttpException
*/
public static function routeBinder($value)
{
2016-09-16 05:07:45 -05:00
if (auth()->check()) {
2016-09-16 05:15:58 -05:00
$model = self::where('key', $value)->where('user_id', auth()->user()->id)->first();
2016-06-10 14:00:00 -05:00
if (!is_null($model)) {
return $model;
}
}
throw new NotFoundHttpException;
}
/**
* @param int $count
*/
public function addStepsDone(int $count)
{
$status = $this->extended_status;
$status['steps_done'] += $count;
$this->extended_status = $status;
$this->save();
}
/**
* @param int $count
*/
public function addTotalSteps(int $count)
{
$status = $this->extended_status;
$status['total_steps'] += $count;
$this->extended_status = $status;
$this->save();
}
2016-06-10 14:00:00 -05:00
/**
* @param $status
*/
public function change($status)
{
$this->status = $status;
$this->save();
}
2016-06-24 07:24:34 -05:00
/**
* @param $value
*
* @return mixed
*/
public function getConfigurationAttribute($value)
{
2016-09-24 10:22:42 -05:00
if (is_null($value)) {
return [];
}
2016-06-24 07:24:34 -05:00
if (strlen($value) == 0) {
return [];
}
2016-06-27 08:15:46 -05:00
return json_decode($value, true);
2016-06-24 07:24:34 -05:00
}
/**
* @param $value
*
* @return mixed
*/
public function getExtendedStatusAttribute($value)
{
if (strlen($value) == 0) {
return [];
}
return json_decode($value, true);
}
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
/**
* @param $value
*/
public function setExtendedStatusAttribute($value)
{
$this->attributes['extended_status'] = json_encode($value);
}
/**
* @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
*/
public function uploadFileContents(): string
{
$fileName = $this->key . '.upload';
$disk = Storage::disk('upload');
$encryptedContent = $disk->get($fileName);
$content = Crypt::decrypt($encryptedContent);
Log::debug(sprintf('Content size is %d bytes.', $content));
2016-06-27 08:15:46 -05:00
return $content;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('FireflyIII\User');
}
2016-06-10 14:00:00 -05:00
}