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