Completed
Push — master ( a76360...3b8ebd )
by Manuel
04:51 queued 03:32
created

ResolveOptionsCommand::handle()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 2
nop 2
1
<?php
2
3
namespace Oscer\Cms\Core\Commands;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Collection;
8
use Oscer\Cms\Core\Options\Models\Option;
9
use Oscer\Cms\Frontend\Contracts\Theme;
10
11
class ResolveOptionsCommand extends Command
12
{
13
    protected $signature = 'cms:options:resolve';
14
15
    protected $description = 'Resolve all options from the configuration and the theme';
16
17
    /**
18
     * Execute the console command.
19
     *
20
     * @return void
21
     */
22
    public function handle(Theme $theme, Repository $config)
23
    {
24
        $resolvedOptions = new Collection();
25
        $tabsWithOptions = collect(array_merge(
26
            $config->get('cms.options'),
27
            ['theme' => $theme->getOptions()]
28
        ));
29
        $tabsWithOptions->each(function (array $options, string $tab) use ($resolvedOptions) {
30
            foreach ($options as $key => $specification) {
31
                $resolvedOptions->add(Option::query()->updateOrCreate(['key' => "{$tab}.{$key}"], [
32
                    'key' => "{$tab}.{$key}",
33
                    'type' => $specification['type'],
34
                    'label' => isset($specification['label']) ? $specification['label'] : null,
35
                ]));
36
            }
37
        });
38
39
        /** @var Collection $resolvedOptionKeys */
40
        $resolvedOptionKeys = $resolvedOptions->map->key;
0 ignored issues
show
Bug introduced by
The property key does not seem to exist in Illuminate\Support\HigherOrderCollectionProxy.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
41
42
        if ($resolvedOptions->count() < Option::query()->count()) {
43
            Option::all()->filter(function (Option $option) use ($resolvedOptionKeys) {
44
                return ! $resolvedOptionKeys->contains($option->key);
45
            })->each->delete();
46
        }
47
    }
48
}
49