Issues (3)

src/Console/Commands/AddDatabaseConfigCommand.php (3 issues)

Labels
1
<?php
2
3
namespace Koomai\LaravelConfig\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Koomai\LaravelConfig\DatabaseConfig;
7
8
class AddDatabaseConfigCommand extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'config:add 
16
                            {name : The config namespace, e.g. database, dashboard} 
17
                            {key : Attribute name – you can use Laravel "dot" notation for nested attributes}
18
                            {value* : Attribute value - pass multiple values separated by space for an array}
19
                            {--reset-cache : Resets config cache}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Add a configuration key/value pair in the database';
27
28
    /**
29
     * Execute the console command.
30
     *
31
     * @return mixed
32
     */
33 4
    public function handle()
34
    {
35 4
        $name = $this->argument('name');
36 4
        $key = $this->argument('key');
37 4
        $value = self::parseValue($this->argument('value'));
0 ignored issues
show
It seems like $this->argument('value') can also be of type null and string; however, parameter $valueAsArray of Koomai\LaravelConfig\Con...igCommand::parseValue() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $value = self::parseValue(/** @scrutinizer ignore-type */ $this->argument('value'));
Loading history...
38
39 4
        $config = DatabaseConfig::find($name);
40
41 4
        if (! $config) {
42 2
            $attributes = [];
43 2
            $newConfig = new DatabaseConfig;
44 2
            $newConfig->name = $name;
0 ignored issues
show
The property name does not seem to exist on Koomai\LaravelConfig\DatabaseConfig. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
45 2
            $newConfig->value = data_set($attributes, $key, $value);
0 ignored issues
show
The property value does not seem to exist on Koomai\LaravelConfig\DatabaseConfig. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
46 2
            $newConfig->save();
47
        } else {
48 2
            $currentConfig = $config->value;
49 2
            data_set($currentConfig, $key, $value);
50
51 2
            $config->value = $currentConfig;
52 2
            $config->save();
53
        }
54
55 4
        $this->invalidateCache();
56 4
        $this->info("Config for [{$name}.{$key}] with value [".implode(',', (array) $value).'] added successfully');
57
58 4
        return 0;
59
    }
60
61
    /**
62
     * Allows arrays to be set as config values. But if there is only
63
     * one item in the array, it returns the value as a primitive.
64
     *
65
     * @param $valueAsArray
66
     *
67
     * @return mixed
68
     */
69 4
    private static function parseValue(array $valueAsArray)
70
    {
71 4
        return (count($valueAsArray) > 1)
72 1
            ? $valueAsArray
73 4
            : reset($valueAsArray);
74
    }
75
76
    /**
77
     * Flushes cache for database config
78
     * If --reset-cache option is passed, it will also re-cache app config.
79
     *
80
     * @return void
81
     */
82 4
    private function invalidateCache(): void
83
    {
84 4
        app('cache')->forget(config('database-config.cache_key'));
85
86 4
        if ($this->option('reset-cache')) {
87
            $this->call('config:cache');
88
        }
89 4
    }
90
}
91