Files
firefly-iii/app/Import/Configuration/FileConfigurator.php

197 lines
5.6 KiB
PHP
Raw Normal View History

<?php
/**
* FileConfigurator.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Configuration;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
use FireflyIII\Support\Import\Configuration\File\Initial;
use FireflyIII\Support\Import\Configuration\File\Map;
use FireflyIII\Support\Import\Configuration\File\Roles;
use FireflyIII\Support\Import\Configuration\File\Upload;
use Log;
/**
* Class FileConfigurator.
*/
class FileConfigurator implements ConfiguratorInterface
{
2017-11-15 12:25:49 +01:00
/** @var ImportJob */
private $job;
2017-07-29 08:27:39 +02:00
/** @var string */
private $warning = '';
/**
* ConfiguratorInterface constructor.
*/
public function __construct()
{
}
/**
* Store any data from the $data array into the job.
*
* @param array $data
*
* @return bool
2017-11-15 12:25:49 +01:00
*
* @throws FireflyException
*/
public function configureJob(array $data): bool
{
$class = $this->getConfigurationClass();
$job = $this->job;
/** @var ConfigurationInterface $object */
$object = new $class($this->job);
$object->setJob($job);
2017-07-29 08:27:39 +02:00
$result = $object->storeConfiguration($data);
$this->warning = $object->getWarningMessage();
2017-07-29 08:27:39 +02:00
return $result;
}
/**
* Return the data required for the next step in the job configuration.
*
* @return array
2017-11-15 12:25:49 +01:00
*
* @throws FireflyException
*/
public function getNextData(): array
{
$class = $this->getConfigurationClass();
$job = $this->job;
/** @var ConfigurationInterface $object */
$object = app($class);
$object->setJob($job);
return $object->getData();
}
/**
* @return string
2017-11-15 12:25:49 +01:00
*
* @throws FireflyException
*/
public function getNextView(): string
{
if (!$this->job->configuration['has-file-upload']) {
return 'import.file.upload';
}
if (!$this->job->configuration['initial-config-complete']) {
return 'import.file.initial';
}
if (!$this->job->configuration['column-roles-complete']) {
return 'import.file.roles';
}
if (!$this->job->configuration['column-mapping-complete']) {
return 'import.file.map';
}
throw new FireflyException('No view for state');
}
2017-07-29 08:27:39 +02:00
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return $this->warning;
}
/**
* @return bool
*/
public function isJobConfigured(): bool
{
2017-06-24 08:37:09 +02:00
$config = $this->job->configuration;
$config['has-file-upload'] = $config['has-file-upload'] ?? false;
2017-06-24 08:37:09 +02:00
$config['initial-config-complete'] = $config['initial-config-complete'] ?? false;
$config['column-roles-complete'] = $config['column-roles-complete'] ?? false;
$config['column-mapping-complete'] = $config['column-mapping-complete'] ?? false;
$this->job->configuration = $config;
$this->job->save();
if ($config['initial-config-complete']
&& $config['column-roles-complete']
&& $config['column-mapping-complete']
&& $config['has-file-upload']
) {
return true;
}
return false;
}
/**
* @param ImportJob $job
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
2017-11-15 12:25:49 +01:00
if (null === $this->job->configuration || 0 === count($this->job->configuration)) {
Log::debug(sprintf('Gave import job %s initial configuration.', $this->job->key));
$this->job->configuration = config('csv.default_config');
$this->job->save();
}
}
/**
* @return string
2017-11-15 12:25:49 +01:00
*
* @throws FireflyException
*/
private function getConfigurationClass(): string
{
$class = false;
switch (true) {
case !$this->job->configuration['has-file-upload']:
$class = Upload::class;
break;
2017-11-15 12:25:49 +01:00
case !$this->job->configuration['initial-config-complete']:
2017-06-14 20:13:19 +02:00
$class = Initial::class;
break;
2017-11-15 12:25:49 +01:00
case !$this->job->configuration['column-roles-complete']:
2017-06-14 20:13:19 +02:00
$class = Roles::class;
break;
2017-11-15 12:25:49 +01:00
case !$this->job->configuration['column-mapping-complete']:
2017-06-14 20:13:19 +02:00
$class = Map::class;
break;
default:
break;
}
2017-11-15 12:25:49 +01:00
if (false === $class || 0 === strlen($class)) {
throw new FireflyException('Cannot handle current job state in getConfigurationClass().');
}
if (!class_exists($class)) {
throw new FireflyException(sprintf('Class %s does not exist in getConfigurationClass().', $class));
}
return $class;
}
2017-07-07 08:09:42 +02:00
}