mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-11 13:42:49 +01:00
ba75a44eb8
* Adopt Laravel coding style The Laravel framework adopts the PSR-2 coding style with some additions. Laravel apps *should* adopt this coding style as well. However, Shift allows you to customize the adopted coding style by adding your own [PHP CS Fixer][1] `.php_cs` config to your project. You may use [Shift's .php_cs][2] file as a base. [1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer [2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200 * Shift bindings PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser. * Shift core files * Shift to Throwable * Add laravel/ui dependency * Unindent vendor mail templates * Shift config files * Default config files In an effort to make upgrading the constantly changing config files easier, Shift defaulted them so you can review the commit diff for changes. Moving forward, you should use ENV variables or create a separate config file to allow the core config files to remain automatically upgradeable. * Shift Laravel dependencies * Shift cleanup * Upgrade to Laravel 7 Co-authored-by: Laravel Shift <shift@laravelshift.com>
134 lines
3.4 KiB
PHP
134 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
*
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
*
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
*
|
|
* @license https://opensource.org/licenses/AAL
|
|
*/
|
|
|
|
namespace App\Jobs\Util;
|
|
|
|
use App\Exceptions\MigrationValidatorFailed;
|
|
use App\Exceptions\NonExistingMigrationFile;
|
|
use App\Exceptions\ProcessingMigrationArchiveFailed;
|
|
use App\Exceptions\ResourceDependencyMissing;
|
|
use App\Exceptions\ResourceNotAvailableForMigration;
|
|
use App\Libraries\MultiDB;
|
|
use App\Mail\MigrationFailed;
|
|
use App\Models\Company;
|
|
use App\Models\User;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
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 $tries = 1;
|
|
|
|
public $timeout = 86400;
|
|
|
|
public $retryAfter = 86430;
|
|
|
|
public function __construct($filepath, User $user, Company $company)
|
|
{
|
|
$this->filepath = $filepath;
|
|
$this->user = $user;
|
|
$this->company = $company;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
* @throws ProcessingMigrationArchiveFailed
|
|
* @throws NonExistingMigrationFile
|
|
*/
|
|
public function handle()
|
|
{
|
|
set_time_limit(0);
|
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
auth()->login($this->user, false);
|
|
|
|
auth()->user()->setCompany($this->company);
|
|
|
|
$this->company->setMigration(true);
|
|
|
|
$zip = new \ZipArchive();
|
|
$archive = $zip->open($this->filepath);
|
|
|
|
$filename = pathinfo($this->filepath, PATHINFO_FILENAME);
|
|
|
|
try {
|
|
if (! $archive) {
|
|
throw new ProcessingMigrationArchiveFailed('Processing migration archive failed. Migration file is possibly corrupted.');
|
|
}
|
|
|
|
$zip->extractTo(storage_path("migrations/{$filename}"));
|
|
$zip->close();
|
|
|
|
if (app()->environment() == 'testing') {
|
|
return true;
|
|
}
|
|
|
|
$this->company->setMigration(true);
|
|
|
|
$file = storage_path("migrations/$filename/migration.json");
|
|
|
|
if (! file_exists($file)) {
|
|
throw new NonExistingMigrationFile('Migration file does not exist, or it is corrupted.');
|
|
}
|
|
|
|
$data = json_decode(file_get_contents($file), 1);
|
|
|
|
Import::dispatchNow($data, $this->company, $this->user);
|
|
|
|
$this->company->setMigration(false);
|
|
} catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) {
|
|
$this->company->setMigration(false);
|
|
|
|
Mail::to($this->user)->send(new MigrationFailed($e, $e->getMessage()));
|
|
|
|
if (app()->environment() !== 'production') {
|
|
info($e->getMessage());
|
|
}
|
|
}
|
|
|
|
//always make sure we unset the migration as running
|
|
|
|
return true;
|
|
}
|
|
|
|
public function failed($exception = null)
|
|
{
|
|
}
|
|
}
|