Passed
Push — master ( e954fa...6d5981 )
by Richard
15:28 queued 11s
created

ExportStoryCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Riclep\Storyblok\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\Str;
8
use Storyblok\ManagementClient;
9
10
class ExportStoryCommand extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'ls:export {slug}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Save a story as JSON';
25
26
	/**
27
	 * Create a new command instance.
28
	 *
29
	 * @return void
30
	 */
31
	public function __construct()
32
	{
33
		$this->client = new ManagementClient(config('storyblok.oauth_token'));
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
35
		parent::__construct();
36
	}
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return int
42
     */
43
    public function handle()
44
    {
45
	    $storyExists = $this->client->get('spaces/' . config('storyblok.space_id') . '/stories/', [
46
			'with_slug' => $this->argument('slug')
47
	    ])->getBody()['stories'];
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

47
	    ])->/** @scrutinizer ignore-call */ getBody()['stories'];

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...
48
49
		if ($storyExists) {
50
			$filename = 'storyblok-' . Str::of($this->argument('slug'))->replace('/', '-')->slug() . '.json';
0 ignored issues
show
Bug introduced by
It seems like $this->argument('slug') can also be of type array; however, parameter $string of Illuminate\Support\Str::of() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
			$filename = 'storyblok-' . Str::of(/** @scrutinizer ignore-type */ $this->argument('slug'))->replace('/', '-')->slug() . '.json';
Loading history...
51
52
			$story = $this->client->get('spaces/' . config('storyblok.space_id') . '/stories/' . $storyExists[0]['id'])->getBody();
53
54
			$json = json_encode($story);
55
56
			Storage::put($filename, $json);
57
58
			$this->info('Saved to storage: ' . $filename);
59
		} else {
60
			$this->warn('There is no story for your slug: ' . $this->argument('slug'));
61
		}
62
    }
63
}
64