-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigureCmsCommand.php
More file actions
136 lines (121 loc) · 5.76 KB
/
Copy pathConfigureCmsCommand.php
File metadata and controls
136 lines (121 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Code16\OzuClient\Console;
use Closure;
use Code16\OzuClient\Client;
use Code16\OzuClient\OzuCms\Form\OzuField;
use Code16\OzuClient\OzuCms\List\OzuColumn;
use Code16\OzuClient\OzuCms\OzuCollectionConfig;
use Code16\OzuClient\OzuCms\OzuCollectionFormConfig;
use Code16\OzuClient\OzuCms\OzuCollectionListConfig;
use Code16\OzuClient\OzuCms\Storage\OzuAbstractCustomStorage;
use Illuminate\Console\Command;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Schema;
class ConfigureCmsCommand extends Command
{
protected $signature = 'ozu:configure-cms';
protected $description = 'Send CMS configuration to Ozu.';
public function handle(Client $ozuClient): int
{
if (empty(config('ozu-client.collections'))) {
$this->info('No collections to configure.');
return self::SUCCESS;
}
collect(config('ozu-client.collections'))
->map(function ($collection, $k) {
$model = match (true) {
is_string($collection) => app($collection),
$collection instanceof Closure => $collection(),
default => $collection,
};
$collection = $model::configureOzuCollection(new OzuCollectionConfig);
$list = $model::configureOzuCollectionList(new OzuCollectionListConfig);
$form = $model::configureOzuCollectionForm(new OzuCollectionFormConfig);
return [
'key' => $model->ozuCollectionKey(),
'label' => $collection->label(),
'icon' => $collection->icon(),
'hasPublicationState' => $collection->hasPublicationState(),
'autoDeployDateField' => $collection->autoDeployDateField(),
'isCreatable' => $collection->isCreatable(),
'isDeletable' => $collection->isDeletable(),
'order' => $k + 1,
'list' => [
'isReorderable' => $list->isReorderable(),
'isSearchable' => $list->isSearchable(),
'isPaginated' => $list->isPaginated(),
'defaultSort' => $list->defaultSort(),
'belongsToFilter' => $list->belongsToFilter()?->toArray(),
'columns' => $list
->columns()
->map(fn (OzuColumn $column) => [
'type' => $column->type(),
'key' => $column->key(),
'label' => $column->label(),
'size' => $column->size(),
]),
],
'form' => [
'title' => $form->titleField()?->toArray(),
'cover' => $form->coverField()?->toArray(),
'content' => $form->contentField()?->toArray(),
'fields' => $form
->customFields()
->map(fn (OzuField $field) => $field->toArray()),
],
'customFields' => collect(Schema::getColumnListing($model->getTable()))
->filter(fn (string $column) => ! in_array($column, $model::$ozuColumns))
->mapWithKeys(fn (string $column) => [
$column => match (Schema::getColumnType($model->getTable(), $column)) {
'datetime', 'timestamps' => 'dateTime',
'date' => 'date',
'int', 'bigint', 'smallint', 'mediumint', 'tinyint' => 'integer',
'float', 'double' => 'float',
'text', 'json' => 'text',
default => 'string',
},
]),
];
})
->each(function (array $collection) use ($ozuClient) {
$this->info('Update CMS configuration for ['.$collection['key'].'].');
try {
$ozuClient->updateCollectionSharpConfiguration(
$collection['key'],
$collection
);
} catch (RequestException $e) {
if ($message = $e->response->json()) {
if (! isset($message['message'])) {
throw $e;
}
$this->error('['.$collection['key'].'] '.$message['message']);
} else {
throw $e;
}
}
});
if (config('ozu-client.custom_storage')) {
$customStorage = config('ozu-client.custom_storage');
if ($customStorage instanceof OzuAbstractCustomStorage) {
if ($customStorage->meetRequirements()) {
$ozuClient->updateCustomStorageConfiguration(config('ozu-client.custom_storage')->toArray());
} else {
$this->error(
sprintf('Custom storage does not meet requirements. Missing : %s',
$customStorage->whatsMissing()->join(', ')
)
);
return self::FAILURE;
}
} else {
$this->error('Custom storage must be an instance of OzuAbstractCustomStorage');
return self::FAILURE;
}
} else {
$ozuClient->updateCustomStorageConfiguration([]);
}
$this->info('CMS configuration sent to Ozu.');
return self::SUCCESS;
}
}