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