CommandUtils   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 41
rs 10
ccs 0
cts 18
cp 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A persistConfig() 0 23 3
A writeConfig() 0 14 1
1
<?php
2
3
namespace Bmatovu\AirtelMoney\Traits;
4
5
trait CommandUtils
6
{
7
    protected function persistConfig(string $configKey, ?string $value): void
8
    {
9
        $this->laravel->make('config')->set([$configKey => $value]);
10
11
        if ($this->option('no-write')) {
0 ignored issues
show
Bug introduced by
It seems like option() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

11
        if ($this->/** @scrutinizer ignore-call */ option('no-write')) {
Loading history...
12
            return;
13
        }
14
15
        $envPath = app()->basePath('.env');
0 ignored issues
show
introduced by
The method basePath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

15
        $envPath = app()->/** @scrutinizer ignore-call */ basePath('.env');
Loading history...
16
17
        $envKey = strtoupper(preg_replace('/[^A-Za-z0-9]+/', '_', $configKey));
18
19
        $pattern = '/^'.preg_quote($envKey, '/').'=["\']?.*/m';
20
21
        $contents = file_get_contents($envPath);
22
23
        if (preg_match($pattern, $contents)) {
24
            $contents = preg_replace($pattern, "{$envKey}=\"{$value}\"", $contents);
25
        } else {
26
            $contents .= PHP_EOL."{$envKey}=\"{$value}\"";
27
        }
28
29
        file_put_contents($envPath, $contents);
30
    }
31
32
    protected function writeConfig(
33
        string $key,
34
        ?string $value = null,
35
        string $config = 'airtel-money'
36
    ): ?string {
37
        $configKey = "{$config}.{$key}";
38
39
        $oldValue = $value ?? $this->laravel->make('config')->get($configKey);
40
41
        $newValue = $this->ask($key, $oldValue);
0 ignored issues
show
Bug introduced by
It seems like ask() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

41
        /** @scrutinizer ignore-call */ 
42
        $newValue = $this->ask($key, $oldValue);
Loading history...
42
43
        $this->persistConfig($configKey, $newValue);
44
45
        return $newValue;
46
    }
47
}
48