Passed
Pull Request — master (#15)
by Chase
02:47
created

ComponentViewCommand::handle()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 17
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 27
rs 9.7
1
<?php
2
3
namespace Riclep\Storyblok\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
8
class ComponentViewCommand extends Command
9
{
10
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'ls:component-list
17
	            {--additional-fields= : Additional fields to pull form Storyblok Management API}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'List all storyblok components.';
25
26
    /**
27
     * Create a new command instance.
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return void
38
     */
39
    public function handle()
40
    {
41
        $managementClient = new \Storyblok\ManagementClient(config('storyblok.oauth_token'));
42
43
        $resp = $managementClient->get('spaces/'.config('storyblok.space_id').'/components');
44
        $components = collect($resp->getBody()['components']);
0 ignored issues
show
Bug introduced by
The method getBody() does not exist on stdClass. ( Ignorable by Annotation )

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

44
        $components = collect($resp->/** @scrutinizer ignore-call */ getBody()['components']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
46
        $additionalFields = $this->option('additional-fields') ?
47
            Str::of($this->option('additional-fields'))->explode(',')
48
            : collect();
49
50
        $rows = $components->map(function ($c) use ($additionalFields) {
51
            $mapped = [
52
                'name' => $c['name'],
53
                'display_name' => $c['display_name'],
54
                'has_image' => $c['image'] ? "<fg=green>true</>" : '<fg=red>false</>',
55
                'has_template' => $c['preview_tmpl'] ? "<fg=green>true</>" : '<fg=red>false</>',
56
            ];
57
58
            $mappedAdditional = collect($c)->only($additionalFields);
59
60
            return array_merge($mapped, $mappedAdditional->toArray());
61
        });
62
63
        $this->table(
64
            array_keys($rows->first()),
65
            $rows
66
        );
67
    }
68
69
}
70