label(__('filament.actions.studio.backfill.name')); $this->authorize('update', Studio::class); $this->action(fn (Studio $record, array $data) => $this->handle($record, $data)); } /** * Perform the action on the given models. * * @param Studio $studio * @param array $fields * @return void */ public function handle(Studio $studio, array $fields): void { if ($studio->resources()->doesntExist()) { $this->failedLog(__('filament.actions.studio.backfill.message.resource_required_failure')); return; } $actions = $this->getActions($fields, $studio); try { foreach ($actions as $action) { $result = $action->handle(); if ($result->hasFailed()) { Notification::make() ->body($result->getMessage()) ->warning() ->actions([ NotificationAction::make('mark-as-read') ->button() ->markAsRead(), ]) ->sendToDatabase(Auth::user()); } } } catch (Exception $e) { $this->failedLog($e); } finally { // Try not to upset third-party APIs Sleep::for(rand(3, 5))->second(); } } /** * Get the fields available on the action. * * @param Form $form * @return Form * * @noinspection PhpMissingParentCallCommonInspection */ public function getForm(Form $form): Form { $studio = $this->getRecord(); return $form ->schema([ Section::make(__('filament.actions.studio.backfill.fields.images.name')) ->schema([ Checkbox::make(self::BACKFILL_LARGE_COVER) ->label(__('filament.actions.studio.backfill.fields.images.large_cover.name')) ->helperText(__('filament.actions.studio.backfill.fields.images.large_cover.help')) ->default(fn () => $studio instanceof Studio && $studio->images()->where(Image::ATTRIBUTE_FACET, ImageFacet::COVER_LARGE->value)->doesntExist()), ]), ]); } /** * Get the selected actions for backfilling studios. * * @param array $fields * @param Studio $studio * @return BackfillAction[] */ protected function getActions(array $fields, Studio $studio): array { $actions = []; foreach ($this->getActionMapping($studio) as $field => $action) { if (Arr::get($fields, $field) === true) { $actions[] = $action; } } return $actions; } /** * Get the mapping of actions to their form fields. * * @param Studio $studio * @return array */ protected function getActionMapping(Studio $studio): array { return [ self::BACKFILL_LARGE_COVER => new BackfillLargeCoverImageAction($studio), ]; } }