firefly-iii/app/Export/Processor.php

197 lines
5.3 KiB
PHP
Raw Normal View History

2016-02-04 10:16:16 -06:00
<?php
/**
* Processor.php
2016-04-01 09:44:46 -05:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-02-04 10:16:16 -06:00
*
* 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-02-04 10:16:16 -06:00
*/
declare(strict_types = 1);
2016-02-04 10:16:16 -06:00
namespace FireflyIII\Export;
use FireflyIII\Exceptions\FireflyException;
2016-05-01 08:05:29 -05:00
use FireflyIII\Export\Collector\AttachmentCollector;
2016-10-23 02:44:14 -05:00
use FireflyIII\Export\Collector\JournalCollector;
2016-05-01 08:05:29 -05:00
use FireflyIII\Export\Collector\UploadCollector;
2016-04-26 05:39:29 -05:00
use FireflyIII\Export\Entry\Entry;
2016-02-04 10:16:16 -06:00
use FireflyIII\Models\ExportJob;
2016-04-26 14:40:15 -05:00
use Illuminate\Filesystem\FilesystemAdapter;
2016-02-04 10:16:16 -06:00
use Illuminate\Support\Collection;
2016-10-23 02:44:14 -05:00
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 $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->includeOldUploads = $settings['includeOldUploads'];
$this->job = $settings['job'];
$this->journals = new Collection;
$this->exportEntries = new Collection;
$this->files = new Collection;
}
/**
2016-04-06 09:37:28 -05:00
* @return bool
2016-02-04 10:16:16 -06:00
*/
2016-04-06 09:37:28 -05:00
public function collectAttachments(): bool
2016-02-04 10:16:16 -06:00
{
2016-05-01 08:05:29 -05:00
/** @var AttachmentCollector $attachmentCollector */
$attachmentCollector = app(AttachmentCollector::class, [$this->job]);
2016-10-23 02:44:14 -05:00
$attachmentCollector->setDates($this->settings['startDate'], $this->settings['endDate']);
2016-02-04 10:16:16 -06:00
$attachmentCollector->run();
2016-10-23 02:44:14 -05:00
$this->files = $this->files->merge($attachmentCollector->getEntries());
2016-04-25 11:43:09 -05:00
2016-04-06 09:37:28 -05:00
return true;
2016-02-04 10:16:16 -06:00
}
/**
2016-04-06 09:37:28 -05:00
* @return bool
2016-02-04 10:16:16 -06:00
*/
2016-04-06 09:37:28 -05:00
public function collectJournals(): bool
2016-02-04 10:16:16 -06:00
{
2016-10-23 02:44:14 -05:00
/** @var JournalCollector $collector */
$collector = app(JournalCollector::class, [$this->job]);
$collector->setDates($this->settings['startDate'], $this->settings['endDate']);
$collector->setAccounts($this->settings['accounts']);
$collector->run();
$this->journals = $collector->getEntries();
Log::debug(sprintf('Count %d journals in collectJournals() ', $this->journals->count()));
2016-05-01 02:52:58 -05:00
2016-04-06 09:37:28 -05:00
return true;
2016-02-04 10:16:16 -06:00
}
2016-04-06 09:37:28 -05:00
/**
* @return bool
*/
public function collectOldUploads(): bool
2016-02-04 10:16:16 -06:00
{
2016-05-01 08:05:29 -05:00
/** @var UploadCollector $uploadCollector */
$uploadCollector = app(UploadCollector::class, [$this->job]);
2016-02-04 10:16:16 -06:00
$uploadCollector->run();
2016-10-23 02:44:14 -05:00
$this->files = $this->files->merge($uploadCollector->getEntries());
2016-04-25 11:43:09 -05:00
2016-04-06 09:37:28 -05:00
return true;
2016-02-04 10:16:16 -06:00
}
/**
2016-04-06 09:37:28 -05:00
* @return bool
2016-02-04 10:16:16 -06:00
*/
2016-04-06 09:37:28 -05:00
public function convertJournals(): bool
2016-02-04 10:16:16 -06:00
{
$count = 0;
2016-10-23 02:44:14 -05:00
foreach ($this->journals as $object) {
$this->exportEntries->push(Entry::fromObject($object));
$count++;
2016-02-04 10:16:16 -06:00
}
2016-10-23 02:44:14 -05:00
Log::debug(sprintf('Count %d entries in exportEntries (convertJournals)', $this->exportEntries->count()));
2016-04-25 11:43:09 -05:00
2016-04-06 09:37:28 -05:00
return true;
2016-02-04 10:16:16 -06:00
}
2016-04-06 09:37:28 -05:00
/**
* @return bool
* @throws FireflyException
*/
public function createZipFile(): bool
2016-02-04 10:16:16 -06:00
{
$zip = new ZipArchive;
2016-02-23 00:27:29 -06:00
$file = $this->job->key . '.zip';
$fullPath = storage_path('export') . '/' . $file;
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);
2016-05-20 01:57:45 -05:00
$zip->addFromString($zipFileName, $disk->get($entry));
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:
2016-04-26 14:40:15 -05:00
$this->deleteFiles($disk);
2016-04-25 11:43:09 -05:00
2016-04-06 09:37:28 -05:00
return true;
2016-02-04 10:16:16 -06:00
}
/**
2016-04-06 09:37:28 -05:00
* @return bool
2016-02-04 10:16:16 -06:00
*/
2016-04-06 09:37:28 -05:00
public function exportJournals(): bool
2016-02-04 10:16:16 -06:00
{
2016-04-26 14:40:15 -05:00
$exporterClass = config('firefly.export_formats.' . $this->exportFormat);
2016-02-04 10:16:16 -06:00
$exporter = app($exporterClass, [$this->job]);
$exporter->setEntries($this->exportEntries);
$exporter->run();
$this->files->push($exporter->getFileName());
2016-04-25 11:43:09 -05:00
2016-04-06 09:37:28 -05:00
return true;
2016-02-04 10:16:16 -06:00
}
/**
* @return Collection
*/
2016-04-06 09:37:28 -05:00
public function getFiles(): Collection
2016-02-04 10:16:16 -06:00
{
return $this->files;
}
2016-04-26 14:40:15 -05:00
/**
* @param FilesystemAdapter $disk
*/
private function deleteFiles(FilesystemAdapter $disk)
{
foreach ($this->getFiles() as $file) {
$disk->delete($file);
}
}
2016-02-10 09:01:18 -06:00
}