Completed
Push — settings ( 96499a...bc400b )
by Tony
03:04
created

MigrateSettings::handle()   C

Complexity

Conditions 8
Paths 21

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 35
rs 5.3846
cc 8
eloc 19
nc 21
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\DbConfig;
6
use Config;
7
use Illuminate\Console\Command;
8
use Settings;
9
10
class MigrateSettings extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'librenms:migrate-settings
18
    {--force} : Force the migration. WARNING: May lose some of your settings.
19
    {--dry-run} : Print changes that would be made, but do not commit them.';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Import settings from config.php that existed in the database previously into the database.  (The database now overrides config.php)';
27
28
    /**
29
     * Create a new command instance.
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        if (Settings::get('settings.migrated')) {
44
            $this->info(trans('commands.migrate-settings.migrated'));
0 ignored issues
show
Bug introduced by
It seems like trans('commands.migrate-settings.migrated') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Illuminate\Console\Command::info() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
            if (!$this->option('force')) {
46
                return;
47
            }
48
            $this->warn(trans('commands.migrate-settings.migrated_warning'));
0 ignored issues
show
Bug introduced by
It seems like trans('commands.migrate-...ings.migrated_warning') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Illuminate\Console\Command::warn() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
49
        }
50
51
        $settings = DbConfig::pluck('config_name');
52
53
        foreach ($settings as $key) {
54
            $config_key = '_config.'.$key;
55
56
            if (Config::has($config_key)) {
57
                $old = Settings::get($key);
58
                $new = Config::get($config_key);
59
60
                if ($old !== $new) {
61
                    $this->info(trans('commands.migrate-settings.migrating', ['setting' => $key, 'old' => $old, 'new' => $new]));
0 ignored issues
show
Bug introduced by
It seems like trans('commands.migrate-...> $old, 'new' => $new)) targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Illuminate\Console\Command::info() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
62
63
                    if (!$this->option('dry-run')) {
64
                        // we aren't authenticated so we can't write through the Settings facade
65
                        DbConfig::updateOrCreate(['config_name' => $key], ['config_value' => $new]);
0 ignored issues
show
Bug introduced by
The method updateOrCreate() does not exist on App\Models\DbConfig. Did you maybe mean create()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
66
                    }
67
                }
68
            }
69
        }
70
71
        if (!$this->option('dry-run')) {
72
            Settings::flush();  // clear the settings cache
73
            DbConfig::updateOrCreate(['config_name' => 'settings.migrated'], ['config_value' => true]);
0 ignored issues
show
Bug introduced by
The method updateOrCreate() does not exist on App\Models\DbConfig. Did you maybe mean create()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
74
        }
75
    }
76
}
77