2020-01-23 21:35:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Util;
|
|
|
|
|
|
|
|
use App\Models\User;
|
2020-02-24 22:15:07 +01:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Libraries\MultiDB;
|
2020-01-23 21:35:00 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
2020-02-24 22:15:07 +01:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
2020-01-23 21:35:00 +01:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2020-02-24 22:15:07 +01:00
|
|
|
use App\Exceptions\ProcessingMigrationArchiveFailed;
|
2020-01-23 21:35:00 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2020-02-19 21:44:12 +01:00
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
|
2020-01-23 21:35:00 +01:00
|
|
|
$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();
|
2020-02-24 22:15:07 +01:00
|
|
|
|
2020-02-25 23:36:11 +01:00
|
|
|
if (app()->environment() !== 'testing') {
|
|
|
|
$this->start($filename);
|
|
|
|
}
|
2020-01-23 21:35:00 +01:00
|
|
|
} else {
|
|
|
|
throw new ProcessingMigrationArchiveFailed();
|
|
|
|
}
|
|
|
|
} catch (ProcessingMigrationArchiveFailed $e) {
|
|
|
|
// TODO: Break the code, stop the migration.. send an e-mail.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rest of the migration..
|
|
|
|
}
|
2020-02-25 23:36:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main method to start the migration.
|
|
|
|
*/
|
|
|
|
protected function start(string $filename): void
|
|
|
|
{
|
|
|
|
$file = storage_path("migrations/$filename/migration.json");
|
|
|
|
|
|
|
|
if (!file_exists($file))
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$handle = fopen($file, "r");
|
|
|
|
$file = fread($handle, filesize($file));
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
$data = json_decode($file, 1);
|
|
|
|
Import::dispatchNow($data, $this->company, $this->user);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
info('Migration failed. Handle this.'); // TODO: Handle the failed job.
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 21:35:00 +01:00
|
|
|
}
|