-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncDashboardFilamentWidgetSettingsAction.php
More file actions
209 lines (174 loc) · 6.62 KB
/
Copy pathSyncDashboardFilamentWidgetSettingsAction.php
File metadata and controls
209 lines (174 loc) · 6.62 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
declare(strict_types=1);
namespace Capell\Admin\Actions;
use Capell\Admin\Contracts\DashboardSettingsContributor;
use Capell\Admin\Enums\DashboardEnum;
use Capell\Admin\Enums\FilamentWidgetEnum;
use Capell\Admin\Facades\CapellAdmin;
use Capell\Admin\Settings\AdminSettings;
use Exception;
use Illuminate\Support\Collection;
use Lorisleiva\Actions\Concerns\AsFake;
use Lorisleiva\Actions\Concerns\AsObject;
final class SyncDashboardFilamentWidgetSettingsAction
{
use AsFake;
use AsObject;
/** @var list<string> */
private const array REMOVED_WIDGET_KEYS = [
'capell_info',
];
public function handle(
bool $repairFullyDisabledDefaults = false,
bool $forceEnableDefaults = false,
bool $repairOverEnabledDefaults = false,
): AdminSettings {
PersistMissingSettingsDefaultsAction::run(AdminSettings::class);
$settings = AdminSettings::instance();
$enabledWidgets = $settings->enabled_widgets;
$knownKeys = $this->knownWidgetKeys();
$defaultEnabledKeys = $this->defaultEnabledWidgetKeys();
if ($forceEnableDefaults || ($repairFullyDisabledDefaults && $this->allKnownDefaultsAreDisabled($enabledWidgets, $defaultEnabledKeys))) {
foreach ($defaultEnabledKeys as $defaultKey) {
$enabledWidgets[$defaultKey] = true;
}
}
if ($repairOverEnabledDefaults && $this->allOptionalWidgetsAreEnabled($enabledWidgets, $knownKeys, $defaultEnabledKeys)) {
foreach (array_diff($knownKeys, $defaultEnabledKeys) as $optionalKey) {
$enabledWidgets[$optionalKey] = false;
}
}
foreach ($this->promotedDefaultWidgetKeys() as $promotedDefaultKey) {
if (in_array($promotedDefaultKey, $defaultEnabledKeys, true)) {
$enabledWidgets[$promotedDefaultKey] = true;
}
}
foreach ($knownKeys as $knownKey) {
if (! array_key_exists($knownKey, $enabledWidgets)) {
$enabledWidgets[$knownKey] = in_array($knownKey, $defaultEnabledKeys, true);
}
}
foreach (self::REMOVED_WIDGET_KEYS as $removedWidgetKey) {
unset($enabledWidgets[$removedWidgetKey]);
}
$hasChanged = false;
if ($enabledWidgets !== $settings->enabled_widgets) {
$settings->enabled_widgets = $enabledWidgets;
$hasChanged = true;
}
$widgetOrder = array_replace(AdminSettings::defaultWidgetOrder(), $settings->widget_order);
foreach (self::REMOVED_WIDGET_KEYS as $removedWidgetKey) {
unset($widgetOrder[$removedWidgetKey]);
}
if ($widgetOrder !== $settings->widget_order) {
$settings->widget_order = $widgetOrder;
$hasChanged = true;
}
if ($hasChanged) {
$settings->save();
}
return $settings->refresh();
}
/**
* @return list<string>
*/
private function knownWidgetKeys(): array
{
return array_values(collect()
->merge($this->contributedWidgetKeys())
->merge(CapellAdmin::getOverviewStatKeys())
->merge($this->registeredDashboardFilamentWidgetKeys())
->merge($this->enumWidgetKeys())
->filter(fn (string $settingsKey): bool => $settingsKey !== '')
->unique()
->values()
->all());
}
/** @return list<string> */
private function promotedDefaultWidgetKeys(): array
{
return [
'extensions.stats',
];
}
/**
* @return list<string>
*/
private function defaultEnabledWidgetKeys(): array
{
return array_values(collect(array_keys(AdminSettings::defaultWidgetOrder()))
->merge(CapellAdmin::getDefaultEnabledOverviewStatKeys())
->filter(fn (string $settingsKey): bool => $settingsKey !== '')
->unique()
->values()
->all());
}
/**
* @return Collection<int, string>
*/
private function contributedWidgetKeys(): Collection
{
/** @var iterable<DashboardSettingsContributor> $contributors */
$contributors = app()->tagged(DashboardSettingsContributor::TAG);
return collect($contributors)
->flatMap(fn (DashboardSettingsContributor $contributor): array => $contributor->settingsKeys())
->pluck('key')
->filter(fn (mixed $settingsKey): bool => is_string($settingsKey));
}
/**
* @return Collection<int, string>
*/
private function registeredDashboardFilamentWidgetKeys(): Collection
{
return collect(DashboardEnum::cases())
->flatMap(fn (DashboardEnum $dashboard): array => CapellAdmin::getDashboardFilamentWidgets($dashboard))
->map(fn (string $widgetClass): string => $this->settingsKeyFor($widgetClass));
}
/**
* @return Collection<int, string>
*/
private function enumWidgetKeys(): Collection
{
return collect(FilamentWidgetEnum::cases())
->map(fn (FilamentWidgetEnum $widgetEnum): string => $this->settingsKeyFor($widgetEnum->value));
}
private function settingsKeyFor(string $widgetClass): string
{
if (! class_exists($widgetClass) || ! method_exists($widgetClass, 'settingsKey')) {
return '';
}
try {
$settingsKey = $widgetClass::settingsKey();
} catch (Exception) {
return '';
}
return is_string($settingsKey) ? $settingsKey : '';
}
/**
* @param array<string, bool> $enabledWidgets
* @param list<string> $defaultKeys
*/
private function allKnownDefaultsAreDisabled(array $enabledWidgets, array $defaultKeys): bool
{
$knownValues = array_intersect_key($enabledWidgets, array_flip($defaultKeys));
if ($knownValues === []) {
return false;
}
return collect($knownValues)->every(fn (bool $enabled): bool => $enabled === false);
}
/**
* @param array<string, bool> $enabledWidgets
* @param list<string> $knownKeys
* @param list<string> $defaultEnabledKeys
*/
private function allOptionalWidgetsAreEnabled(array $enabledWidgets, array $knownKeys, array $defaultEnabledKeys): bool
{
$optionalKeys = array_values(array_diff($knownKeys, $defaultEnabledKeys));
if ($optionalKeys === []) {
return false;
}
$optionalValues = array_intersect_key($enabledWidgets, array_flip($optionalKeys));
return $optionalValues !== []
&& collect($optionalValues)->every(fn (bool $enabled): bool => $enabled);
}
}