Passed
Push — master ( 659bb2...377f81 )
by Richard
10:24 queued 13s
created

StubViewsCommand::handle()   C

Complexity

Conditions 12
Paths 1

Size

Total Lines 52
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 39
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 52
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Riclep\Storyblok\Console;
4
5
use Illuminate\Console\Command;
6
7
class StubViewsCommand extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'ls:stub-views {--O|overwrite}';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Command description';
22
23
    /**
24
     * Create a new command instance.
25
     *
26
     * @return void
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct();
31
    }
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return int
37
     */
38
    public function handle()
39
    {
40
		$client = new \Storyblok\ManagementClient(config('storyblok.oauth_token'));
41
42
		$components = collect($client->get('spaces/' . config('storyblok.space_id') . '/components/')->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

42
		$components = collect($client->get('spaces/' . config('storyblok.space_id') . '/components/')->/** @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...
43
44
		$components->each(function ($component) {
45
			$path = resource_path('views/' . str_replace('.', '/', config('storyblok.view_path')) . 'blocks/');
46
			$filename =  $component['name'] . '.blade.php';
47
48
			if ($this->option('overwrite') || !file_exists($path . $filename)) {
49
				$content = file_get_contents(__DIR__ . '/stubs/blade.stub');
50
				$content = str_replace('#NAME#', $component['name'], $content);
51
52
				$body = '';
53
54
				foreach ($component['schema'] as $name => $field) {
55
					switch ($field['type']) {
56
						case 'bloks':
57
							$body .= "\t" . '@foreach ($block->' . $name . ' as $childBlock)' . "\n";
58
							$body .= "\t\t" . '{{ $childBlock->render() }}' . "\n";
59
							$body .= "\t" . '@endforeach' . "\n";
60
							break;
61
						case 'datetime':
62
						case 'number':
63
						case 'text':
64
							$body .= "\t" . '<p>{{ $block->' . $name . ' }}</p>' . "\n";
65
							break;
66
						case 'multilink':
67
							$body .= "\t" . '<a href="{{ $block->' . $name . '->cached_url }}"></a>' . "\n";
68
						break;
69
						case 'options':
70
							$body .= "\t" . '@foreach ($block->' . $name . ' as $childBlock)' . "\n";
71
							$body .= "\t\t\n";
72
							$body .= "\t" . '@endforeach' . "\n";
73
							break;
74
						case 'textarea':
75
						case 'richtext':
76
							$body .= "\t" . '<div>{!! $block->' . $name . ' !!}</div>' . "\n";
77
						break;
78
						default:
79
							$body .= "\t" . '{{ $block->' . $name . ' }}' . "\n";
80
					}
81
82
					$body .= "\n";
83
				}
84
85
				$content = str_replace('#BODY#', $body, $content);
86
87
				file_put_contents($path . $filename, $content);
88
89
				$this->info('Created: '. $component['name'] . '.blade.php');
90
			}
91
		});
92
    }
93
}
94