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

ImportStoryCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 56
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 23 2
1
<?php
2
3
namespace Riclep\Storyblok\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
use Storyblok\ManagementClient;
8
9
class ImportStoryCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'ls:import {filename} {slug}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Import a story from JSON - it will be created in your space’s root';
24
25
	/**
26
	 * Create a new command instance.
27
	 *
28
	 * @return void
29
	 */
30
	public function __construct()
31
	{
32
		$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...
33
34
		parent::__construct();
35
	}
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return int
41
     */
42
    public function handle()
43
    {
44
	    $storyExists = $this->client->get('spaces/' . config('storyblok.space_id') . '/stories/', [
45
			'with_slug' => $this->argument('slug')
46
	    ])->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

46
	    ])->/** @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...
47
48
		if (!$storyExists) {
49
			$source = json_decode(Storage::get($this->argument('filename')), true);
0 ignored issues
show
Bug introduced by
It seems like $this->argument('filename') can also be of type array; however, parameter $path of Illuminate\Support\Facades\Storage::get() 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

49
			$source = json_decode(Storage::get(/** @scrutinizer ignore-type */ $this->argument('filename')), true);
Loading history...
50
51
			$story = [
52
				"story" =>  [
53
					"name" => $source['story']['name'] . ' (Imported)',
54
					"slug" => $this->argument('slug'),
55
					"content" => $source['story']['content'],
56
				],
57
				"publish" =>  1
58
			];
59
60
			$importedStory = $this->client->post('spaces/' . config('storyblok.space_id') . '/stories/', $story)->getBody()['story'];
61
62
			$this->info('Imported into Storyblok: ' . $importedStory['name']);
63
		} else {
64
			$this->warn('Story already exists for: ' . $this->argument('slug'));
65
		}
66
    }
67
}
68