InstallCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 21
c 1
b 0
f 0
dl 0
loc 73
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 9 2
A makeComponents() 0 12 3
A __construct() 0 8 1
1
<?php
2
3
namespace Riclep\StoryblokForms\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\File;
7
use Riclep\StoryblokForms\Support\ComponentGroupMaker;
8
use Riclep\StoryblokForms\Support\ComponentMaker;
9
use Storyblok\ManagementClient;
10
11
class InstallCommand extends Command
12
{
13
	// TODO - refactor to use Laravel Storyblok CLI package
14
15
	/**
16
	 * @var string
17
	 */
18
	protected $name  = 'lsf:install';
19
20
	/**
21
	 * @var string
22
	 */
23
	protected $description = 'Create the required components for Storyblok forms';
24
25
	/**
26
	 * @var ManagementClient
27
	 */
28
	protected $managementClient;
29
30
	/**
31
	 * @var array
32
	 */
33
	protected $componentGroups = [];
34
35
36
	/**
37
	 *
38
	 */
39
	public function __construct()
40
	{
41
		parent::__construct();
42
43
        $this->managementClient = new ManagementClient(
44
            apiKey: config('storyblok.oauth_token'),
45
            apiEndpoint: config('storyblok.management_api_base_url'),
46
            ssl: config('storyblok.use_ssl')
47
        );
48
	}
49
50
	/**
51
	 * Execute the console command.
52
	 *
53
	 * @return void
54
	 */
55
	public function handle()
56
	{
57
		if (config('storyblok.oauth_token')) {
58
			(new ComponentGroupMaker($this))->import();
59
60
			$this->makeComponents();
61
62
		} else {
63
			$this->error('Please set your management token in the Storyblok config file');
64
		}
65
66
	}
67
68
69
	/**
70
	 * @throws \JsonException
71
	 */
72
	protected function makeComponents(): void
73
	{
74
		// TODO - allow publishing and using custom stubs
75
		$templates = File::allFiles(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR  . '..' . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'components');
76
77
		foreach ($templates as $template) {
78
			$json = file_get_contents($template->getRealPath());
79
80
			$schema = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
81
82
			if ($schema) {
83
				(new ComponentMaker($this, $schema))->import();
84
			}
85
		}
86
	}
87
}
88