|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Riclep\StoryblokForms\Console\InstallCommand; |
|
7
|
|
|
use Riclep\StoryblokForms\Traits\GetsComponentGroups; |
|
8
|
|
|
use Storyblok\ManagementClient; |
|
9
|
|
|
|
|
10
|
|
|
class ComponentGroupMaker |
|
11
|
|
|
{ |
|
12
|
|
|
// TODO - refactor to use Laravel Storyblok CLI package |
|
13
|
|
|
|
|
14
|
|
|
use GetsComponentGroups; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Collection A list of all component groups |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $componentGroups; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ManagementClient Storyblok Management Client |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $managementClient; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var InstallCommand The package’s command |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $command; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param InstallCommand $command |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(InstallCommand $command) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->command = $command; |
|
37
|
|
|
|
|
38
|
|
|
$this->managementClient = new ManagementClient( |
|
39
|
|
|
apiKey: config('storyblok.oauth_token'), |
|
40
|
|
|
apiEndpoint: config('storyblok.management_api_base_url'), |
|
41
|
|
|
ssl: config('storyblok.use_ssl') |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the import started |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
* @throws \Storyblok\ApiException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function import(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$this->getGroups(); |
|
54
|
|
|
|
|
55
|
|
|
$groups = config('storyblok-forms.component_groups'); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($groups as $group) { |
|
58
|
|
|
$this->createGroup($group); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Creates a new Component Group in Storyblok |
|
64
|
|
|
* |
|
65
|
|
|
* @param $group |
|
66
|
|
|
* @return void |
|
67
|
|
|
* @throws \Storyblok\ApiException |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function createGroup($group): void |
|
70
|
|
|
{ |
|
71
|
|
|
if (!$this->componentGroups->has($group)) { |
|
72
|
|
|
$response = $this->managementClient->post('spaces/'.config('storyblok.space_id').'/component_groups', [ |
|
73
|
|
|
'component_group' => [ |
|
74
|
|
|
'name' => $group |
|
75
|
|
|
] |
|
76
|
|
|
]); |
|
77
|
|
|
$this->componentGroups = $this->componentGroups->merge(collect($response->getBody())->keyBy('name')); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$this->command->info('Created group: ' . $group); |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->command->warn($group. 'component group already exists'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |