ldc-legaulois/app/Services/Picto/PictoService.php
2026-09-25 16:25:31 +02:00

41 lines
1 KiB
PHP

<?php
namespace App\Services\Picto;
use App\Models\Picto;
use App\Models\SectionPage;
class PictoService
{
/**
* Create a new picto for the given section.
*
* Returns the created Picto on success, or an error message string
* when the section has reached its maximum number of pictos.
*/
public function store(array $data): Picto | string
{
$section = SectionPage::findOrFail($data['section_id']);
$sectionData = json_decode(json_encode($section->data), true) ?? [];
$maxPictos = $sectionData['max_pictos'] ?? 5;
$count = Picto::where('section_id', $section->id)->count();
if ($count >= $maxPictos) {
return "Le nombre maximum de pictos ($maxPictos) est atteint pour cette section.";
}
return Picto::create($data);
}
/**
* Update the given picto with the provided data.
*/
public function update(Picto $picto, array $data): Picto | string
{
$picto->update($data);
return $picto;
}
}