33 lines
821 B
PHP
33 lines
821 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace DishPlanner\UserDish\Actions;
|
||
|
|
|
||
|
|
use App\Models\WeeklyRecurrence;
|
||
|
|
use DishPlanner\UserDish\Exceptions\InvalidRecurrenceTypeException;
|
||
|
|
use DishPlanner\UserDish\Interfaces\RecurrenceInterface;
|
||
|
|
use Illuminate\Support\Arr;
|
||
|
|
|
||
|
|
class UpdateFixedRecurrenceAction
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* @throws InvalidRecurrenceTypeException
|
||
|
|
*/
|
||
|
|
public function execute(RecurrenceInterface $recurrence, array $data): RecurrenceInterface
|
||
|
|
{
|
||
|
|
if (! $recurrence instanceof WeeklyRecurrence) {
|
||
|
|
throw new InvalidRecurrenceTypeException();
|
||
|
|
}
|
||
|
|
|
||
|
|
$weekday = Arr::get($data, 'recurrence_data.weekday');
|
||
|
|
|
||
|
|
if ($recurrence->weekday === $weekday) {
|
||
|
|
return $recurrence;
|
||
|
|
}
|
||
|
|
|
||
|
|
$recurrence->weekday = $weekday;
|
||
|
|
$recurrence->save();
|
||
|
|
|
||
|
|
return $recurrence;
|
||
|
|
}
|
||
|
|
}
|