Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
14 / 15 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| PromptPersonalMilestoneService | |
93.33% |
14 / 15 |
|
85.71% |
6 / 7 |
7.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAll | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getById | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| invalidateCache | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Services; |
| 4 | |
| 5 | use App\Http\Models\PromptPersonalMilestone; |
| 6 | use App\Http\Repositories\interfaces\IPromptPersonalMilestoneRepository; |
| 7 | use Illuminate\Support\Collection; |
| 8 | use Illuminate\Support\Facades\Cache; |
| 9 | |
| 10 | /** |
| 11 | * Service for prompt personal milestone business logic. |
| 12 | * |
| 13 | * Handles all business logic related to prompt personal milestone configurations, |
| 14 | * coordinating with the repository for data access and managing caching. |
| 15 | */ |
| 16 | class PromptPersonalMilestoneService |
| 17 | { |
| 18 | /** |
| 19 | * Cache key for all prompt personal milestones. |
| 20 | */ |
| 21 | private const CACHE_KEY_ALL = 'prompt_personal_milestone:all'; |
| 22 | |
| 23 | /** |
| 24 | * Cache TTL in seconds (1 hour). |
| 25 | */ |
| 26 | private const CACHE_TTL = 3600; |
| 27 | |
| 28 | public function __construct( |
| 29 | private IPromptPersonalMilestoneRepository $promptPersonalMilestoneRepository |
| 30 | ) {} |
| 31 | |
| 32 | /** |
| 33 | * Get all prompt personal milestones ordered by newest first. |
| 34 | * |
| 35 | * @return Collection<int, PromptPersonalMilestone> Collection of prompt personal milestones |
| 36 | */ |
| 37 | public function getAll(): Collection |
| 38 | { |
| 39 | return Cache::remember(self::CACHE_KEY_ALL, self::CACHE_TTL, function () { |
| 40 | return $this->promptPersonalMilestoneRepository->getAll(); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Find a prompt personal milestone by its ID. |
| 46 | * |
| 47 | * @param string $id The prompt personal milestone ID |
| 48 | * @return PromptPersonalMilestone|null The milestone or null if not found |
| 49 | */ |
| 50 | public function getById(string $id): ?PromptPersonalMilestone |
| 51 | { |
| 52 | return $this->promptPersonalMilestoneRepository->findById($id); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Create a new prompt personal milestone. |
| 57 | * |
| 58 | * @param array{title: string, prompt: string} $data The milestone data |
| 59 | * @return PromptPersonalMilestone The created milestone |
| 60 | */ |
| 61 | public function create(array $data): PromptPersonalMilestone |
| 62 | { |
| 63 | $milestone = $this->promptPersonalMilestoneRepository->create($data); |
| 64 | |
| 65 | $this->invalidateCache(); |
| 66 | |
| 67 | return $milestone; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Update an existing prompt personal milestone. |
| 72 | * |
| 73 | * @param PromptPersonalMilestone $promptPersonalMilestone The milestone to update |
| 74 | * @param array{title?: string, prompt?: string} $data The update data |
| 75 | * @return PromptPersonalMilestone The updated milestone |
| 76 | */ |
| 77 | public function update(PromptPersonalMilestone $promptPersonalMilestone, array $data): PromptPersonalMilestone |
| 78 | { |
| 79 | $promptPersonalMilestone = $this->promptPersonalMilestoneRepository->update($promptPersonalMilestone, $data); |
| 80 | |
| 81 | $this->invalidateCache(); |
| 82 | |
| 83 | return $promptPersonalMilestone; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Delete a prompt personal milestone. |
| 88 | * |
| 89 | * @param PromptPersonalMilestone $promptPersonalMilestone The milestone to delete |
| 90 | * @return bool True if deleted successfully |
| 91 | */ |
| 92 | public function delete(PromptPersonalMilestone $promptPersonalMilestone): bool |
| 93 | { |
| 94 | $result = $this->promptPersonalMilestoneRepository->delete($promptPersonalMilestone); |
| 95 | |
| 96 | $this->invalidateCache(); |
| 97 | |
| 98 | return $result; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Invalidate prompt personal milestone caches. |
| 103 | */ |
| 104 | private function invalidateCache(): void |
| 105 | { |
| 106 | Cache::forget(self::CACHE_KEY_ALL); |
| 107 | } |
| 108 | } |