adrema/app/Activity/Actions/ActivityUpdateAction.php

78 lines
2.4 KiB
PHP
Raw Normal View History

2023-02-17 01:50:44 +01:00
<?php
namespace App\Activity\Actions;
use App\Activity;
use App\Subactivity;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
2023-02-17 14:54:35 +01:00
/**
* @template Payload of array{name: string, subactivities: array<int, int>}
*/
2023-02-17 01:50:44 +01:00
class ActivityUpdateAction
{
use AsAction;
2023-02-17 14:54:35 +01:00
/**
* @param Payload $payload
*/
2023-02-17 01:50:44 +01:00
public function handle(Activity $activity, array $payload): void
{
2023-03-04 12:09:25 +01:00
DB::transaction(function () use ($activity, $payload) {
2023-02-17 01:50:44 +01:00
$activity->update($payload);
$activity->subactivities()->sync($payload['subactivities']);
});
}
2023-02-17 14:54:35 +01:00
/**
* @return array<string, string>
*/
2023-02-17 01:50:44 +01:00
public function rules(): array
{
return [
'name' => 'required|max:255',
2023-03-04 12:09:25 +01:00
'is_filterable' => 'present|boolean',
'subactivities' => 'present|array',
2023-02-17 01:50:44 +01:00
];
}
public function asController(ActionRequest $request, Activity $activity): RedirectResponse
{
if ($activity->hasNami) {
$this->validateNami($activity, $request->validated());
}
$this->handle($activity, $request->validated());
return redirect()->route('activity.index');
}
/**
* @todo handle this with a model event on the pivot model
2023-03-04 12:09:25 +01:00
*
2023-02-17 14:54:35 +01:00
* @param Payload $payload
2023-02-17 01:50:44 +01:00
*/
private function validateNami(Activity $activity, array $payload): void
{
2023-02-17 14:54:35 +01:00
if ($activity->name !== $payload['name']) {
2023-02-17 01:50:44 +01:00
throw ValidationException::withMessages(['nami_id' => 'Aktivität ist in NaMi. Update des Namens nicht möglich.']);
}
2023-02-17 14:54:35 +01:00
$removingSubactivities = $activity->subactivities()->whereNotIn('id', $payload['subactivities'])->pluck('id');
2023-02-17 01:50:44 +01:00
if ($removingSubactivities->first(fn ($subactivity) => Subactivity::find($subactivity)->hasNami)) {
throw ValidationException::withMessages(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']);
}
2023-02-17 14:54:35 +01:00
$addingSubactivities = collect($payload['subactivities'])->filter(fn ($subactivityId) => $activity->subactivities->doesntContain($subactivityId));
2023-02-17 01:50:44 +01:00
if ($addingSubactivities->first(fn ($subactivity) => Subactivity::find($subactivity)->hasNami)) {
throw ValidationException::withMessages(['nami_id' => 'Untertätigkeit kann nicht hinzugefügt werden.']);
}
}
}