Files
firefly-iii/app/Support/Import/JobConfiguration/File/NewFileJobHandler.php

208 lines
6.1 KiB
PHP
Raw Normal View History

<?php
/**
* NewFileJobHandler.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2018-05-21 07:22:38 +02:00
namespace FireflyIII\Support\Import\JobConfiguration\File;
2018-05-06 20:42:30 +02:00
use Exception;
use FireflyIII\Exceptions\FireflyException;
2018-05-12 10:46:18 +02:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use Log;
/**
* Class NewFileJobHandler
*/
2018-05-21 09:40:19 +02:00
class NewFileJobHandler implements FileConfigurationInterface
{
2018-05-12 10:46:18 +02:00
/** @var AttachmentHelperInterface */
private $attachments;
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* Store data associated with current stage.
*
* @param array $data
*
* @throws FireflyException
* @return MessageBag
*/
public function configureJob(array $data): MessageBag
{
// nothing to store, validate upload
// and push to next stage.
$messages = $this->validateAttachments();
if ($messages->count() > 0) {
return $messages;
}
// store config if it's in one of the attachments.
$this->storeConfiguration();
2018-05-06 20:42:30 +02:00
// set file type in config:
$config = $this->repository->getConfiguration($this->importJob);
2018-05-06 20:42:30 +02:00
$config['file-type'] = $data['import_file_type'];
$this->repository->setConfiguration($this->importJob, $config);
$this->repository->setStage($this->importJob, 'configure-upload');
return new MessageBag();
}
2018-05-06 20:42:30 +02:00
/**
*
2018-05-12 10:46:18 +02:00
* Get the data necessary to show the configuration screen.
2018-06-06 21:23:00 +02:00
*
2018-05-12 10:46:18 +02:00
* @codeCoverageIgnore
2018-05-06 20:42:30 +02:00
* @return array
*/
public function getNextData(): array
{
/** @var array $allowedTypes */
$allowedTypes = config('import.options.file.import_formats');
2018-05-06 20:42:30 +02:00
$importFileTypes = [];
$defaultImportType = config('import.options.file.default_import_format');
foreach ($allowedTypes as $type) {
2018-05-06 20:42:30 +02:00
$importFileTypes[$type] = trans('import.import_file_type_' . $type);
}
return [
'default_type' => $defaultImportType,
'file_types' => $importFileTypes,
];
}
/**
* @param ImportJob $job
*/
2018-05-12 15:50:01 +02:00
public function setImportJob(ImportJob $importJob): void
2018-05-06 20:42:30 +02:00
{
2018-05-12 15:50:01 +02:00
$this->importJob = $importJob;
2018-05-12 10:46:18 +02:00
$this->repository = app(ImportJobRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
2018-05-12 15:50:01 +02:00
$this->repository->setUser($importJob->user);
}
/**
* Store config from job.
*
* @throws FireflyException
*/
public function storeConfiguration(): void
{
/** @var Collection $attachments */
2018-05-12 10:46:18 +02:00
$attachments = $this->repository->getAttachments($this->importJob);
/** @var Attachment $attachment */
foreach ($attachments as $attachment) {
// if file is configuration file, store it into the job.
if ($attachment->filename === 'configuration_file') {
$this->storeConfig($attachment);
}
}
}
/**
* Check if all attachments are UTF8.
*
* @return MessageBag
* @throws FireflyException
*/
public function validateAttachments(): MessageBag
{
$messages = new MessageBag;
/** @var Collection $attachments */
2018-05-12 10:46:18 +02:00
$attachments = $this->repository->getAttachments($this->importJob);
/** @var Attachment $attachment */
foreach ($attachments as $attachment) {
// check if content is UTF8:
if (!$this->isUTF8($attachment)) {
$message = trans('import.file_not_utf8');
Log::error($message);
$messages->add('import_file', $message);
// delete attachment:
try {
$attachment->delete();
2018-05-12 10:46:18 +02:00
// @codeCoverageIgnoreStart
} catch (Exception $e) {
throw new FireflyException(sprintf('Could not delete attachment: %s', $e->getMessage()));
}
2018-05-12 10:46:18 +02:00
// @codeCoverageIgnoreEnd
return $messages;
}
// if file is configuration file, store it into the job.
if ($attachment->filename === 'configuration_file') {
$this->storeConfig($attachment);
}
}
return $messages;
}
/**
* @param Attachment $attachment
*
* @return bool
*/
private function isUTF8(Attachment $attachment): bool
{
2018-05-12 10:46:18 +02:00
$content = $this->attachments->getAttachmentContent($attachment);
$result = mb_detect_encoding($content, 'UTF-8', true);
if ($result === false) {
return false;
}
if ($result !== 'ASCII' && $result !== 'UTF-8') {
2018-05-12 10:46:18 +02:00
return false; // @codeCoverageIgnore
}
return true;
}
2018-05-12 10:46:18 +02:00
/**
* Take attachment, extract config, and put in job.\
*
* @param Attachment $attachment
*
* @throws FireflyException
*/
private function storeConfig(Attachment $attachment): void
{
$content = $this->attachments->getAttachmentContent($attachment);
$json = json_decode($content, true);
if (null !== $json) {
$this->repository->setConfiguration($this->importJob, $json);
}
}
2018-05-11 09:51:47 +02:00
}