akaunting/app/Jobs/Install/FinishUpdate.php

136 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2020-12-24 02:16:00 +03:00
<?php
namespace App\Jobs\Install;
use App\Abstracts\Job;
2021-09-10 09:55:54 +03:00
use App\Interfaces\Listener\ShouldUpdateAllCompanies;
2020-12-24 02:16:00 +03:00
use App\Models\Module\Module;
2022-02-14 18:00:44 +03:00
use App\Traits\Modules;
2020-12-24 02:16:00 +03:00
use App\Utilities\Console;
2022-03-02 12:02:34 +03:00
use App\Utilities\Versions;
2021-09-09 22:41:44 +03:00
use Illuminate\Filesystem\Filesystem;
2022-02-28 19:42:33 +03:00
use Illuminate\Support\Facades\Artisan;
2021-09-10 09:45:11 +03:00
use Illuminate\Support\Facades\File;
2020-12-24 02:16:00 +03:00
class FinishUpdate extends Job
{
2022-02-14 18:00:44 +03:00
use Modules;
2020-12-24 02:16:00 +03:00
protected $alias;
protected $new;
protected $old;
2020-12-25 12:08:15 +03:00
protected $company_id;
2020-12-24 02:16:00 +03:00
/**
* Create a new job instance.
*
* @param $alias
* @param $new
* @param $old
2020-12-25 12:08:15 +03:00
* @param $company_id
2020-12-24 02:16:00 +03:00
*/
2020-12-25 12:08:15 +03:00
public function __construct($alias, $new, $old, $company_id)
2020-12-24 02:16:00 +03:00
{
$this->alias = $alias;
$this->new = $new;
$this->old = $old;
2020-12-25 12:08:15 +03:00
$this->company_id = $company_id;
2020-12-24 02:16:00 +03:00
}
2021-09-09 22:41:44 +03:00
public function handle(): void
2020-12-24 02:16:00 +03:00
{
2022-02-14 18:00:44 +03:00
$this->authorize();
2021-09-09 22:41:44 +03:00
$companies = $this->getCompanies();
2020-12-24 02:16:00 +03:00
foreach ($companies as $company) {
2021-01-16 01:57:36 +03:00
$company_id = is_object($company) ? $company->company_id : $company;
2020-12-24 02:16:00 +03:00
$command = "update:finish {$this->alias} {$company_id} {$this->new} {$this->old}";
if (true !== $result = Console::run($command)) {
$message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $this->alias]);
throw new \Exception($message);
}
}
}
2021-09-09 22:41:44 +03:00
2022-02-14 18:00:44 +03:00
/**
* Determine if this action is applicable.
*/
public function authorize(): void
{
2022-02-14 20:52:26 +03:00
if (($this->alias != 'core') && ! $this->moduleExists($this->alias)) {
2022-02-14 18:00:44 +03:00
throw new \Exception("Module [{$this->alias}] not found.");
}
}
2021-09-09 22:41:44 +03:00
public function getCompanies()
{
if ($this->alias == 'core') {
return [$this->company_id];
}
return $this->getCompaniesOfModule();
}
public function getCompaniesOfModule()
{
$listener = $this->getListenerTypeOfModule();
if ($listener == 'none') {
2022-02-28 19:42:33 +03:00
Artisan::call('cache:clear');
2021-09-09 22:41:44 +03:00
return [];
}
if ($listener == 'one') {
return [$this->company_id];
}
// Get company list from modules table
return Module::alias($this->alias)->allCompanies()->cursor();
}
public function getListenerTypeOfModule(): string
{
$listener = 'none';
$module = module($this->alias);
$filesystem = app(Filesystem::class);
$updates_folder = $module->getPath() . '/Listeners/Update';
2021-09-10 09:45:11 +03:00
if (! File::isDirectory($updates_folder)) {
return $listener;
}
2021-09-09 22:41:44 +03:00
foreach ($filesystem->allFiles($updates_folder) as $file) {
$path = str_replace([$module->getPath(), '.php'], '', $file->getPathname());
// Thank you PSR-4
$class = '\Modules\\' . $module->getStudlyName() . str_replace('/', '\\', $path);
2022-03-02 12:02:34 +03:00
if (! Versions::shouldUpdate($class::VERSION, $this->old, $this->new)) {
2021-09-09 22:41:44 +03:00
continue;
}
if (app($class) instanceof ShouldUpdateAllCompanies) {
// Going to update data
$listener = 'all';
break;
} else {
// Going to update tables/files
$listener = 'one';
}
}
return $listener;
}
2020-12-24 02:16:00 +03:00
}