styling and docstring fixes

This commit is contained in:
Rev 2021-10-13 12:40:45 +09:00
parent befed27c20
commit 241788a92e

View File

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