Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
74.19% |
23 / 31 |
|
80.00% |
8 / 10 |
CRAP | |
0.00% |
0 / 1 |
| ParameterService | |
74.19% |
23 / 31 |
|
80.00% |
8 / 10 |
13.08 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCacheTtl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAll | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getByName | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getById | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| delete | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| invalidateCaches | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMetadataParameters | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Services; |
| 4 | |
| 5 | use App\Http\Models\Parameter; |
| 6 | use App\Http\Repositories\ParameterRepository; |
| 7 | use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
| 8 | use Illuminate\Support\Collection; |
| 9 | use Illuminate\Support\Facades\Cache; |
| 10 | use Illuminate\Support\Facades\Config; |
| 11 | |
| 12 | /** |
| 13 | * Service for parameter business logic. |
| 14 | * |
| 15 | * This service handles all business logic related to parameters, |
| 16 | * coordinating with the repository for data access. Includes Redis |
| 17 | * caching for improved read performance. |
| 18 | */ |
| 19 | class ParameterService |
| 20 | { |
| 21 | /** |
| 22 | * Cache key prefix for parameters. |
| 23 | */ |
| 24 | private const CACHE_KEY_PREFIX = 'parameters:'; |
| 25 | |
| 26 | public function __construct( |
| 27 | private ParameterRepository $parameterRepository, |
| 28 | private CacheInvalidationService $cacheInvalidationService |
| 29 | ) {} |
| 30 | |
| 31 | /** |
| 32 | * Get the cache TTL from config. |
| 33 | */ |
| 34 | private function getCacheTtl(): int |
| 35 | { |
| 36 | return Config::get('cache.ttl.parameters', 3600); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get all parameters with optional filtering and pagination. |
| 41 | * |
| 42 | * Note: Pagination queries are not cached due to varying parameters. |
| 43 | * |
| 44 | * @param string|null $filter Optional search filter for parameter names |
| 45 | * @param int $perPage Number of items per page |
| 46 | * @return LengthAwarePaginator<Parameter> |
| 47 | */ |
| 48 | public function getAll(?string $filter = null, int $perPage = 15): LengthAwarePaginator |
| 49 | { |
| 50 | return $this->parameterRepository->getAllPaginated($filter, $perPage); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get a parameter by its name. |
| 55 | * |
| 56 | * Results are cached for improved read performance. |
| 57 | * |
| 58 | * @param string $name The parameter name |
| 59 | * @return Parameter|null The parameter or null if not found |
| 60 | */ |
| 61 | public function getByName(string $name): ?Parameter |
| 62 | { |
| 63 | $cacheKey = self::CACHE_KEY_PREFIX.'name:'.$name; |
| 64 | |
| 65 | return Cache::remember($cacheKey, $this->getCacheTtl(), function () use ($name) { |
| 66 | return $this->parameterRepository->findByName($name); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get a parameter by its ID. |
| 72 | * |
| 73 | * Results are cached for improved read performance. |
| 74 | * |
| 75 | * @param string $id The parameter ID |
| 76 | * @return Parameter|null The parameter or null if not found |
| 77 | */ |
| 78 | public function getById(string $id): ?Parameter |
| 79 | { |
| 80 | $cacheKey = self::CACHE_KEY_PREFIX.'id:'.$id; |
| 81 | |
| 82 | return Cache::remember($cacheKey, $this->getCacheTtl(), function () use ($id) { |
| 83 | return $this->parameterRepository->findById($id); |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Create a new parameter. |
| 89 | * |
| 90 | * @param array{name: string, value: mixed} $data The parameter data |
| 91 | * @return Parameter The created parameter |
| 92 | */ |
| 93 | public function create(array $data): Parameter |
| 94 | { |
| 95 | $parameter = $this->parameterRepository->create($data); |
| 96 | |
| 97 | $this->cacheInvalidationService->invalidateParameterCaches($data['name']); |
| 98 | |
| 99 | return $parameter; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Update an existing parameter. |
| 104 | * |
| 105 | * @param Parameter $parameter The parameter to update |
| 106 | * @param array{name?: string, value?: mixed} $data The update data |
| 107 | * @return Parameter The updated parameter |
| 108 | */ |
| 109 | public function update(Parameter $parameter, array $data): Parameter |
| 110 | { |
| 111 | $oldName = $parameter->name; |
| 112 | $parameter = $this->parameterRepository->update($parameter, $data); |
| 113 | |
| 114 | // Invalidate both old and new name caches if name changed |
| 115 | $newName = $data['name'] ?? $oldName; |
| 116 | $this->cacheInvalidationService->invalidateParameterCaches($oldName, (string) $parameter->_id); |
| 117 | if ($newName !== $oldName) { |
| 118 | $this->cacheInvalidationService->invalidateParameterCaches($newName); |
| 119 | } |
| 120 | |
| 121 | return $parameter; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Delete a parameter. |
| 126 | * |
| 127 | * @param Parameter $parameter The parameter to delete |
| 128 | * @return bool True if deleted successfully |
| 129 | */ |
| 130 | public function delete(Parameter $parameter): bool |
| 131 | { |
| 132 | $name = $parameter->name; |
| 133 | $id = (string) $parameter->_id; |
| 134 | |
| 135 | $result = $this->parameterRepository->delete($parameter); |
| 136 | |
| 137 | $this->cacheInvalidationService->invalidateParameterCaches($name, $id); |
| 138 | |
| 139 | return $result; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Invalidate all parameter caches. |
| 144 | * |
| 145 | * Useful for bulk operations or manual cache clearing. |
| 146 | */ |
| 147 | public function invalidateCaches(): void |
| 148 | { |
| 149 | $this->cacheInvalidationService->invalidateParameterCaches(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get all parameters that should be exposed in metadata API. |
| 154 | * |
| 155 | * Returns parameters where metadata_key is not null. Results are cached |
| 156 | * for improved performance on the high-traffic check-quota endpoint. |
| 157 | * |
| 158 | * @return Collection<int, Parameter> Collection of parameters with metadata_key set |
| 159 | */ |
| 160 | public function getMetadataParameters(): Collection |
| 161 | { |
| 162 | $cacheKey = self::CACHE_KEY_PREFIX.'metadata'; |
| 163 | |
| 164 | return Cache::remember($cacheKey, $this->getCacheTtl(), function () { |
| 165 | return Parameter::whereNotNull('metadata_key')->get(); |
| 166 | }); |
| 167 | } |
| 168 | } |