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

SettingsCache::handle()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 8
Ratio 28.57 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 28
ccs 17
cts 17
cp 1
rs 8.439
cc 5
eloc 16
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 SettingsCache extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'settings:cache {--model=}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Cache all settings';
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('Caching all settings');
43 4
        $settings = Settings::all();
44 4
        $duration = config('settings.cache_duration');
45
46 4
        foreach ($settings as $key => $value) {
47 2
            $cache_key = Settings::cacheKey($key);
48
49 2
            Cache::put($cache_key, $value, $duration);
50
        }
51
52 4
        $model = $this->option('model');
53
54 4
        if (isset($model)) {
55 2
            $users = (new $model)->all();
56 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...
57 2
                $settings = $user->allSettings();
58 2
                foreach ($settings as $key => $value) {
59 2
                    $cache_key = $user->settingsCacheKey($key);
60
61 2
                    Cache::put($cache_key, $value, $duration);
62
                }
63
            }
64
        }
65
66 4
        $this->info('Cached all settings');
67 4
    }
68
}
69