firefly-iii/app/Export/Processor.php

183 lines
5.3 KiB
PHP
Raw Normal View History

2016-02-04 10:16:16 -06:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2016-02-04 10:16:16 -06:00
/**
* Processor.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Export;
use Auth;
use Config;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ExportJob;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
use Log;
2016-02-23 00:27:29 -06:00
use Storage;
2016-02-04 10:16:16 -06:00
use ZipArchive;
/**
* Class Processor
*
* @package FireflyIII\Export
*/
class Processor
{
/** @var Collection */
public $accounts;
/** @var string */
public $exportFormat;
/** @var bool */
public $includeAttachments;
/** @var bool */
public $includeConfig;
/** @var bool */
public $includeOldUploads;
/** @var ExportJob */
public $job;
/** @var array */
public $settings;
/** @var \FireflyIII\Export\ConfigurationFile */
private $configurationMaker;
/** @var Collection */
private $exportEntries;
/** @var Collection */
private $files;
/** @var Collection */
private $journals;
/**
* Processor constructor.
*
* @param array $settings
*/
public function __construct(array $settings)
{
// save settings
$this->settings = $settings;
$this->accounts = $settings['accounts'];
$this->exportFormat = $settings['exportFormat'];
$this->includeAttachments = $settings['includeAttachments'];
$this->includeConfig = $settings['includeConfig'];
$this->includeOldUploads = $settings['includeOldUploads'];
$this->job = $settings['job'];
$this->journals = new Collection;
$this->exportEntries = new Collection;
$this->files = new Collection;
}
/**
*
*/
public function collectAttachments()
{
$attachmentCollector = app('FireflyIII\Export\Collector\AttachmentCollector', [$this->job]);
$attachmentCollector->run();
$this->files = $this->files->merge($attachmentCollector->getFiles());
}
/**
*
*/
public function collectJournals()
{
$args = [$this->accounts, Auth::user(), $this->settings['startDate'], $this->settings['endDate']];
$journalCollector = app('FireflyIII\Repositories\Journal\JournalCollector', $args);
2016-02-04 10:16:16 -06:00
$this->journals = $journalCollector->collect();
Log::debug(
'Collected ' .
$this->journals->count() . ' journals (between ' .
$this->settings['startDate']->format('Y-m-d') . ' and ' .
$this->settings['endDate']->format('Y-m-d')
. ').'
);
2016-02-04 10:16:16 -06:00
}
public function collectOldUploads()
{
$uploadCollector = app('FireflyIII\Export\Collector\UploadCollector', [$this->job]);
$uploadCollector->run();
$this->files = $this->files->merge($uploadCollector->getFiles());
}
/**
*
*/
public function convertJournals()
{
$count = 0;
2016-02-04 10:16:16 -06:00
/** @var TransactionJournal $journal */
foreach ($this->journals as $journal) {
$this->exportEntries->push(Entry::fromJournal($journal));
$count++;
2016-02-04 10:16:16 -06:00
}
Log::debug('Converted ' . $count . ' journals to "Entry" objects.');
2016-02-04 10:16:16 -06:00
}
public function createConfigFile()
{
$this->configurationMaker = app('FireflyIII\Export\ConfigurationFile', [$this->job]);
$this->files->push($this->configurationMaker->make());
}
public function createZipFile()
{
$zip = new ZipArchive;
2016-02-23 00:27:29 -06:00
$file = $this->job->key . '.zip';
$fullPath = storage_path('export') . '/' . $file;
Log::debug('Will create zip file at ' . $fullPath);
2016-02-04 10:16:16 -06:00
2016-02-23 00:27:29 -06:00
if ($zip->open($fullPath, ZipArchive::CREATE) !== true) {
2016-02-04 10:16:16 -06:00
throw new FireflyException('Cannot store zip file.');
}
// for each file in the collection, add it to the zip file.
2016-02-23 00:27:29 -06:00
$disk = Storage::disk('export');
foreach ($this->getFiles() as $entry) {
// is part of this job?
$zipFileName = str_replace($this->job->key . '-', '', $entry);
$result = $zip->addFromString($zipFileName, $disk->get($entry));
2016-02-22 23:54:16 -06:00
if (!$result) {
2016-02-23 00:27:29 -06:00
Log::error('Could not add "' . $entry . '" into zip file as "' . $zipFileName . '".');
2016-02-22 23:54:16 -06:00
}
2016-02-04 10:16:16 -06:00
}
2016-02-23 00:27:29 -06:00
2016-02-04 10:16:16 -06:00
$zip->close();
2016-02-04 10:22:01 -06:00
// delete the files:
foreach ($this->getFiles() as $file) {
Log::debug('Will now delete file "' . $file . '".');
2016-02-23 00:27:29 -06:00
$disk->delete($file);
2016-02-04 10:22:01 -06:00
}
Log::debug('Done!');
2016-02-04 10:16:16 -06:00
}
/**
*
*/
public function exportJournals()
{
$exporterClass = Config::get('firefly.export_formats.' . $this->exportFormat);
$exporter = app($exporterClass, [$this->job]);
Log::debug('Going to export ' . $this->exportEntries->count() . ' export entries into ' . $this->exportFormat . ' format.');
2016-02-04 10:16:16 -06:00
$exporter->setEntries($this->exportEntries);
$exporter->run();
$this->files->push($exporter->getFileName());
Log::debug('Added "' . $exporter->getFileName() . '" to the list of files to include in the zip.');
2016-02-04 10:16:16 -06:00
}
/**
* @return Collection
*/
public function getFiles()
{
return $this->files;
}
2016-02-10 09:01:18 -06:00
}