Completed
Push — dev ( cb6296...2e3e2f )
by Amr
03:50
created

SettingsClear::handle()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 8
Ratio 29.63 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 27
ccs 16
cts 16
cp 1
rs 8.439
cc 5
eloc 15
nc 4
nop 0
crap 5
1
<?php
2
3
namespace Merodiro\Settings\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Cache;
7
use Merodiro\Settings\Facades\Settings;
8
9
class SettingsClear extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'settings:clear {--model=}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Clear settings cache';
24
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
     */
30 44
    public function __construct()
31
    {
32 44
        parent::__construct();
33 44
    }
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40 4
    public function handle()
41
    {
42 4
        $this->comment('Clearing settings cache.');
43 4
        $settings = Settings::all();
44
45 4
        foreach ($settings as $key => $value) {
46 2
            $cache_key = Settings::cacheKey($key);
47
48 2
            Cache::forget($cache_key);
49
        }
50
51 4
        $model = $this->option('model');
52
53 4
        if (isset($model)) {
54 2
            $users = (new $model)->all();
55 2 View Code Duplication
            foreach ($users as $user) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56 2
                $settings = $user->allSettings();
57 2
                foreach ($settings as $key => $value) {
58 2
                    $cache_key = $user->settingsCacheKey($key);
59
60 2
                    Cache::forget($cache_key);
61
                }
62
            }
63
        }
64
65 4
        $this->info('Cleared ' . count($settings) . ' settings cache.');
66 4
    }
67
}
68