DeleteDatabaseConfigCommand::invalidateCache()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
3
namespace Koomai\LaravelConfig\Console\Commands;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Console\Command;
7
use Koomai\LaravelConfig\DatabaseConfig;
8
9
class DeleteDatabaseConfigCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'config:delete 
17
                            {name : The config namespace, e.g. database, dashboard} 
18
                            {key : Attribute name – you can use Laravel "dot" notation for nested attributes}
19
                            {--reset-cache : Resets config cache}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Remove a configuration key/value pair from the database';
27
28
    /**
29
     * Execute the console command.
30
     *
31
     * @return mixed
32
     */
33 3
    public function handle()
34
    {
35 3
        $name = $this->argument('name');
36 3
        $key = $this->argument('key');
37 3
        $config = DatabaseConfig::find($name);
38
39 3
        if (! $config) {
40 1
            $this->error("No configuration for [{$name}] found");
41
42 1
            return 1;
43
        }
44
45 2
        if ($key === '') {
46 1
            $config->delete();
47 1
            $this->info("All config for [{$name}] deleted successfully.");
48 1
            $this->invalidateCache();
49
50 1
            return 0;
51
        }
52
53 1
        $newConfig = Arr::except($config->value, $key);
54 1
        $config->value = $newConfig;
55 1
        $config->save();
56
57 1
        $this->invalidateCache();
58
59 1
        $this->info("Config [{$name}.{$key}] deleted successfully.");
60
61 1
        return 0;
62
    }
63
64
    /**
65
     * Flushes cache for database config
66
     * If --reset-cache option is passed, it will also re-cache app config.
67
     *
68
     * @return void
69
     */
70 2
    private function invalidateCache(): void
71
    {
72 2
        app('cache')->forget(config('database-config.cache_key'));
73
74 2
        if ($this->option('reset-cache')) {
75
            $this->call('config:cache');
76
        }
77 2
    }
78
}
79