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