1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Jobs/Util/StartMigration.php
Benjamin Beganović 8121a0166d
Importing migration data via HTTP (#3365)
* Accept migration over HTTP

* Comment wip tests
2020-02-25 08:15:07 +11:00

82 lines
2.0 KiB
PHP

<?php
namespace App\Jobs\Util;
use App\Models\User;
use App\Models\Company;
use App\Libraries\MultiDB;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Exceptions\ProcessingMigrationArchiveFailed;
class StartMigration implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $filepath;
/**
* @var User
*/
private $user;
/**
* @var Company
*/
private $company;
/**
* Create a new job instance.
*
* @param $filepath
* @param User $user
* @param Company $company
*/
public function __construct($filepath, User $user, Company $company)
{
$this->filepath = $filepath;
$this->user = $user;
$this->company = $company;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->company->db);
$zip = new \ZipArchive();
$archive = $zip->open($this->filepath);
$filename = pathinfo($this->filepath, PATHINFO_FILENAME);
try {
if ($archive) {
$zip->extractTo(storage_path("migrations/{$filename}"));
$zip->close();
$migration_file = storage_path("migrations/$filename/migration.json");
$handle = fopen($migration_file, "r");
$migration_file = fread($handle, filesize($migration_file));
fclose($handle);
$data = json_decode($migration_file,1);
Import::dispatchNow($data, $this->company, $this->user);
} else {
throw new ProcessingMigrationArchiveFailed();
}
} catch (ProcessingMigrationArchiveFailed $e) {
// TODO: Break the code, stop the migration.. send an e-mail.
}
// Rest of the migration..
}
}