refactored module commands
This commit is contained in:
		@@ -2,21 +2,16 @@
 | 
			
		||||
 | 
			
		||||
namespace Akaunting\Module\Commands;
 | 
			
		||||
 | 
			
		||||
use App\Models\Module\Module;
 | 
			
		||||
use App\Models\Module\ModuleHistory;
 | 
			
		||||
use Illuminate\Console\Command;
 | 
			
		||||
use Illuminate\Support\Facades\Cache;
 | 
			
		||||
use Illuminate\Support\Str;
 | 
			
		||||
use Symfony\Component\Console\Input\InputArgument;
 | 
			
		||||
use App\Console\Commands\UninstallModule;
 | 
			
		||||
 | 
			
		||||
class DeleteCommand extends Command
 | 
			
		||||
class DeleteCommand extends UninstallModule
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * The name and signature of the console command.
 | 
			
		||||
     *
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    protected $signature = 'module:delete {alias} {company_id}';
 | 
			
		||||
    protected $signature = 'module:delete {alias} {company} {locale=en-GB}';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The console command description.
 | 
			
		||||
@@ -24,61 +19,4 @@ class DeleteCommand extends Command
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    protected $description = 'Delete the specified module.';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Execute the console command.
 | 
			
		||||
     *
 | 
			
		||||
     * @return mixed
 | 
			
		||||
     */
 | 
			
		||||
    public function handle()
 | 
			
		||||
    {
 | 
			
		||||
        $alias = Str::kebab($this->argument('alias'));
 | 
			
		||||
        $company_id = $this->argument('company_id');
 | 
			
		||||
 | 
			
		||||
        $model = Module::alias($alias)->companyId($company_id)->first();
 | 
			
		||||
 | 
			
		||||
        if (!$model) {
 | 
			
		||||
            $this->info("Module [{$alias}] not found.");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $module = module($alias);
 | 
			
		||||
        $module->delete();
 | 
			
		||||
 | 
			
		||||
        $model->enabled = 0;
 | 
			
		||||
        $model->save();
 | 
			
		||||
 | 
			
		||||
        // Add history
 | 
			
		||||
        $data = [
 | 
			
		||||
            'company_id' => $company_id,
 | 
			
		||||
            'module_id' => $model->id,
 | 
			
		||||
            'category' => $module->get('category', 'payment-method'),
 | 
			
		||||
            'version' => $module->get('version'),
 | 
			
		||||
            'description' => trans('modules.deleted', ['module' => $module->get('alias')]),
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        ModuleHistory::create($data);
 | 
			
		||||
 | 
			
		||||
        // Trigger event
 | 
			
		||||
        event(new \App\Events\Module\Deleted($alias, $company_id));
 | 
			
		||||
 | 
			
		||||
        if (config('module.cache.enabled')) {
 | 
			
		||||
            Cache::forget(config('module.cache.key'));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->info("Module [{$alias}] deleted.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    * Get the console command arguments.
 | 
			
		||||
    *
 | 
			
		||||
    * @return array
 | 
			
		||||
    */
 | 
			
		||||
    protected function getArguments()
 | 
			
		||||
    {
 | 
			
		||||
        return array(
 | 
			
		||||
            array('alias', InputArgument::REQUIRED, 'Module alias.'),
 | 
			
		||||
            array('company_id', InputArgument::REQUIRED, 'Company ID.'),
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,20 +2,16 @@
 | 
			
		||||
 | 
			
		||||
namespace Akaunting\Module\Commands;
 | 
			
		||||
 | 
			
		||||
use App\Models\Module\Module;
 | 
			
		||||
use App\Models\Module\ModuleHistory;
 | 
			
		||||
use Illuminate\Console\Command;
 | 
			
		||||
use Illuminate\Support\Str;
 | 
			
		||||
use Symfony\Component\Console\Input\InputArgument;
 | 
			
		||||
use App\Abstracts\Commands\Module;
 | 
			
		||||
 | 
			
		||||
class DisableCommand extends Command
 | 
			
		||||
class DisableCommand extends Module
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * The name and signature of the console command.
 | 
			
		||||
     *
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    protected $signature = 'module:disable {alias} {company_id}';
 | 
			
		||||
    protected $signature = 'module:disable {alias} {company} {locale=en-GB}';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The console command description.
 | 
			
		||||
@@ -31,52 +27,30 @@ class DisableCommand extends Command
 | 
			
		||||
     */
 | 
			
		||||
    public function handle()
 | 
			
		||||
    {
 | 
			
		||||
        $alias = Str::kebab($this->argument('alias'));
 | 
			
		||||
        $company_id = $this->argument('company_id');
 | 
			
		||||
        $this->prepare();
 | 
			
		||||
 | 
			
		||||
        $model = Module::alias($alias)->companyId($company_id)->first();
 | 
			
		||||
 | 
			
		||||
        if (!$model) {
 | 
			
		||||
            $this->info("Module [{$alias}] not found.");
 | 
			
		||||
        if (!$this->getModel()) {
 | 
			
		||||
            $this->info("Module [{$this->alias}] not found.");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($model->enabled == 1) {
 | 
			
		||||
            $model->enabled = 0;
 | 
			
		||||
            $model->save();
 | 
			
		||||
 | 
			
		||||
            $module = module($alias);
 | 
			
		||||
 | 
			
		||||
            // Add history
 | 
			
		||||
            $data = [
 | 
			
		||||
                'company_id' => $company_id,
 | 
			
		||||
                'module_id' => $model->id,
 | 
			
		||||
                'category' => $module->get('category', 'payment-method'),
 | 
			
		||||
                'version' => $module->get('version'),
 | 
			
		||||
                'description' => trans('modules.disabled', ['module' => $module->get('alias')]),
 | 
			
		||||
            ];
 | 
			
		||||
 | 
			
		||||
            ModuleHistory::create($data);
 | 
			
		||||
 | 
			
		||||
            // Trigger event
 | 
			
		||||
            event(new \App\Events\Module\Disabled($alias, $company_id));
 | 
			
		||||
 | 
			
		||||
            $this->info("Module [{$alias}] disabled.");
 | 
			
		||||
        } else {
 | 
			
		||||
            $this->comment("Module [{$alias}] is already disabled.");
 | 
			
		||||
        if ($this->model->enabled == 0) {
 | 
			
		||||
            $this->comment("Module [{$this->alias}] is already disabled.");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    * Get the console command arguments.
 | 
			
		||||
    *
 | 
			
		||||
    * @return array
 | 
			
		||||
    */
 | 
			
		||||
    protected function getArguments()
 | 
			
		||||
    {
 | 
			
		||||
        return array(
 | 
			
		||||
            array('alias', InputArgument::REQUIRED, 'Module alias.'),
 | 
			
		||||
            array('company_id', InputArgument::REQUIRED, 'Company ID.'),
 | 
			
		||||
        );
 | 
			
		||||
        $this->changeRuntime();
 | 
			
		||||
 | 
			
		||||
        // Update db
 | 
			
		||||
        $this->model->enabled = 0;
 | 
			
		||||
        $this->model->save();
 | 
			
		||||
 | 
			
		||||
        $this->createHistory('disabled');
 | 
			
		||||
 | 
			
		||||
        event(new \App\Events\Module\Disabled($this->alias, $this->company_id));
 | 
			
		||||
 | 
			
		||||
        $this->revertRuntime();
 | 
			
		||||
 | 
			
		||||
        $this->info("Module [{$this->alias}] disabled.");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,20 +2,16 @@
 | 
			
		||||
 | 
			
		||||
namespace Akaunting\Module\Commands;
 | 
			
		||||
 | 
			
		||||
use App\Models\Module\Module;
 | 
			
		||||
use App\Models\Module\ModuleHistory;
 | 
			
		||||
use Illuminate\Console\Command;
 | 
			
		||||
use Illuminate\Support\Str;
 | 
			
		||||
use Symfony\Component\Console\Input\InputArgument;
 | 
			
		||||
use App\Abstracts\Commands\Module;
 | 
			
		||||
 | 
			
		||||
class EnableCommand extends Command
 | 
			
		||||
class EnableCommand extends Module
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * The name and signature of the console command.
 | 
			
		||||
     *
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    protected $signature = 'module:enable {alias} {company_id}';
 | 
			
		||||
    protected $signature = 'module:enable {alias} {company} {locale=en-GB}';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The console command description.
 | 
			
		||||
@@ -31,52 +27,30 @@ class EnableCommand extends Command
 | 
			
		||||
     */
 | 
			
		||||
    public function handle()
 | 
			
		||||
    {
 | 
			
		||||
        $alias = Str::kebab($this->argument('alias'));
 | 
			
		||||
        $company_id = $this->argument('company_id');
 | 
			
		||||
        $this->prepare();
 | 
			
		||||
 | 
			
		||||
        $model = Module::alias($alias)->companyId($company_id)->first();
 | 
			
		||||
 | 
			
		||||
        if (!$model) {
 | 
			
		||||
            $this->info("Module [{$alias}] not found.");
 | 
			
		||||
        if (!$this->getModel()) {
 | 
			
		||||
            $this->info("Module [{$this->alias}] not found.");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($model->enabled == 0) {
 | 
			
		||||
            $model->enabled = 1;
 | 
			
		||||
            $model->save();
 | 
			
		||||
 | 
			
		||||
            $module = module($alias);
 | 
			
		||||
 | 
			
		||||
            // Add history
 | 
			
		||||
            $data = [
 | 
			
		||||
                'company_id' => $company_id,
 | 
			
		||||
                'module_id' => $model->id,
 | 
			
		||||
                'category' => $module->get('category', 'payment-method'),
 | 
			
		||||
                'version' => $module->get('version'),
 | 
			
		||||
                'description' => trans('modules.enabled', ['module' => $module->get('alias')]),
 | 
			
		||||
            ];
 | 
			
		||||
 | 
			
		||||
            ModuleHistory::create($data);
 | 
			
		||||
 | 
			
		||||
            // Trigger event
 | 
			
		||||
            event(new \App\Events\Module\Enabled($alias, $company_id));
 | 
			
		||||
 | 
			
		||||
            $this->info("Module [{$alias}] enabled.");
 | 
			
		||||
        } else {
 | 
			
		||||
            $this->comment("Module [{$alias}] is already enabled.");
 | 
			
		||||
        if ($this->model->enabled == 1) {
 | 
			
		||||
            $this->comment("Module [{$this->alias}] is already enabled.");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    * Get the console command arguments.
 | 
			
		||||
    *
 | 
			
		||||
    * @return array
 | 
			
		||||
    */
 | 
			
		||||
    protected function getArguments()
 | 
			
		||||
    {
 | 
			
		||||
        return array(
 | 
			
		||||
            array('alias', InputArgument::REQUIRED, 'Module alias.'),
 | 
			
		||||
            array('company_id', InputArgument::REQUIRED, 'Company ID.'),
 | 
			
		||||
        );
 | 
			
		||||
        $this->changeRuntime();
 | 
			
		||||
 | 
			
		||||
        // Update db
 | 
			
		||||
        $this->model->enabled = 1;
 | 
			
		||||
        $this->model->save();
 | 
			
		||||
 | 
			
		||||
        $this->createHistory('enabled');
 | 
			
		||||
 | 
			
		||||
        event(new \App\Events\Module\Enabled($this->alias, $this->company_id));
 | 
			
		||||
 | 
			
		||||
        $this->revertRuntime();
 | 
			
		||||
 | 
			
		||||
        $this->info("Module [{$this->alias}] enabled.");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,18 +2,11 @@
 | 
			
		||||
 | 
			
		||||
namespace Akaunting\Module\Commands;
 | 
			
		||||
 | 
			
		||||
use App\Models\Module\Module;
 | 
			
		||||
use App\Models\Module\ModuleHistory;
 | 
			
		||||
use App\Traits\Permissions;
 | 
			
		||||
use Illuminate\Console\Command;
 | 
			
		||||
use Illuminate\Support\Facades\Cache;
 | 
			
		||||
use Illuminate\Support\Str;
 | 
			
		||||
use Symfony\Component\Console\Input\InputArgument;
 | 
			
		||||
use App\Abstracts\Commands\Module;
 | 
			
		||||
use App\Models\Module\Module as Model;
 | 
			
		||||
 | 
			
		||||
class InstallCommand extends Command
 | 
			
		||||
class InstallCommand extends Module
 | 
			
		||||
{
 | 
			
		||||
    use Permissions;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The name and signature of the console command.
 | 
			
		||||
     *
 | 
			
		||||
@@ -35,65 +28,23 @@ class InstallCommand extends Command
 | 
			
		||||
     */
 | 
			
		||||
    public function handle()
 | 
			
		||||
    {
 | 
			
		||||
        $alias = Str::kebab($this->argument('alias'));
 | 
			
		||||
        $company_id = $this->argument('company');
 | 
			
		||||
        $locale = $this->argument('locale');
 | 
			
		||||
        $this->prepare();
 | 
			
		||||
 | 
			
		||||
        $old_company_id = session('company_id');
 | 
			
		||||
        $this->changeRuntime();
 | 
			
		||||
 | 
			
		||||
        session(['company_id' => $company_id]);
 | 
			
		||||
 | 
			
		||||
        app()->setLocale($locale);
 | 
			
		||||
 | 
			
		||||
        $module = module($alias);
 | 
			
		||||
 | 
			
		||||
        $model = Module::create([
 | 
			
		||||
            'company_id' => $company_id,
 | 
			
		||||
            'alias' => $alias,
 | 
			
		||||
        // Create db
 | 
			
		||||
        $this->model = Model::create([
 | 
			
		||||
            'company_id' => $this->company_id,
 | 
			
		||||
            'alias' => $this->alias,
 | 
			
		||||
            'enabled' => '1',
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        ModuleHistory::create([
 | 
			
		||||
            'company_id' => $company_id,
 | 
			
		||||
            'module_id' => $model->id,
 | 
			
		||||
            'category' => $module->get('category', 'payment-method'),
 | 
			
		||||
            'version' => $module->get('version'),
 | 
			
		||||
            'description' => trans('modules.installed', ['module' => $alias]),
 | 
			
		||||
        ]);
 | 
			
		||||
        $this->createHistory('installed');
 | 
			
		||||
 | 
			
		||||
        if (config('module.cache.enabled')) {
 | 
			
		||||
            Cache::forget(config('module.cache.key'));
 | 
			
		||||
        }
 | 
			
		||||
        event(new \App\Events\Module\Installed($this->alias, $this->company_id));
 | 
			
		||||
 | 
			
		||||
        // Disable model cache during installation
 | 
			
		||||
        config(['laravel-model-caching.enabled' => false]);
 | 
			
		||||
 | 
			
		||||
        // Update database
 | 
			
		||||
        $this->call('migrate', ['--force' => true]);
 | 
			
		||||
 | 
			
		||||
        event(new \App\Events\Module\Installed($alias, $company_id));
 | 
			
		||||
 | 
			
		||||
        $this->attachDefaultModulePermissions($module);
 | 
			
		||||
 | 
			
		||||
        session()->forget('company_id');
 | 
			
		||||
 | 
			
		||||
        if (!empty($old_company_id)) {
 | 
			
		||||
            session(['company_id' => $old_company_id]);
 | 
			
		||||
        }
 | 
			
		||||
        $this->revertRuntime();
 | 
			
		||||
 | 
			
		||||
        $this->info('Module installed!');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    * Get the console command arguments.
 | 
			
		||||
    *
 | 
			
		||||
    * @return array
 | 
			
		||||
    */
 | 
			
		||||
    protected function getArguments()
 | 
			
		||||
    {
 | 
			
		||||
        return array(
 | 
			
		||||
            array('alias', InputArgument::REQUIRED, 'Module alias.'),
 | 
			
		||||
            array('company_id', InputArgument::REQUIRED, 'Company ID.'),
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user