diff --git a/app/Data/ItemsCollection.php b/app/Data/ItemsCollection.php index 7aeab9c..2a7bbcd 100644 --- a/app/Data/ItemsCollection.php +++ b/app/Data/ItemsCollection.php @@ -16,16 +16,19 @@ class ItemsCollection private string $items_cache_key = 'items'; private string $locales_cache_key = 'locales'; - public function __construct() { + public function __construct() + { if (!Cache::has($this->items_cache_key)) { $this->refreshItemsCache(); + } else { + $this->items = Cache::get($this->items_cache_key); } - else $this->items = Cache::get($this->items_cache_key); if (!Cache::has($this->locales_cache_key)) { $this->refreshLocalesCache(); + } else { + $this->locales = Cache::get($this->locales_cache_key); } - else $this->locales = Cache::get($this->locales_cache_key); } public function refreshLocalesCache(): void @@ -51,17 +54,29 @@ class ItemsCollection Cache::put($this->locales_cache_key, $this->locales); } - public function refreshItemsCache(): void { + /** + * @return void + */ + public function refreshItemsCache(): void + { $this->items = collect(json_decode(file_get_contents(GiteaConfig::RAW_ITEMS_URL), true)); $this->items = $this->items->merge(collect(json_decode(file_get_contents(GiteaConfig::RAW_CUSTOMIZATION_URL), true))); Cache::put($this->items_cache_key, $this->items); } - public function getLocales(): Collection { + /** + * @return Collection + */ + public function getLocales(): Collection + { return $this->locales->keys(); } - public function refreshAllCache(): void { + /** + * @return void + */ + public function refreshAllCache(): void + { $this->refreshItemsCache(); $this->refreshLocalesCache(); } @@ -69,7 +84,8 @@ class ItemsCollection /** * Checks if the query is in the key */ - private function contains(string $key, string $query) : bool { + private function contains(string $key, string $query): bool + { $key = Str::lower(trim($key)); $query = Str::lower(trim($query)); @@ -81,7 +97,7 @@ class ItemsCollection * @param string $locale the chosen local. Default to 'en' * @return Collection */ - public function findItem(string $query, string $locale = 'en'): Collection + public function findItem(string $query, string $locale): Collection { return $this->items->filter(function ($val, $key) use ($query, $locale) { return $this->contains($val['_id'], $query) @@ -103,7 +119,7 @@ class ItemsCollection * @return array * @throws ItemNotFoundException */ - public function getItemById(string $id, string $locale = 'en'): array + public function getItemById(string $id, string $locale): array { $item = $this->items[$id] ?? throw new ItemNotFoundException('Item not found'); return [