ProApiCache.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Models\Cache;
  3. use App\Services\CallApiService;
  4. use Illuminate\Support\Facades\Storage;
  5. class ProApiCache
  6. {
  7. public function getCachedResponse($url, $filePath)
  8. {
  9. $responseFilePath = $this->getFullFilePath($filePath, 'response');
  10. // 1. $filePath reponse를 먼저 읽어온다.
  11. if (Storage::disk('dabory')->exists($responseFilePath)) {
  12. return json_decode(Storage::disk('dabory')->get($responseFilePath), true);
  13. }
  14. // 2. 없으면 request를 json을 읽어오고
  15. $requestFilePath = $this->getFullFilePath($filePath, 'request');
  16. if (! Storage::disk('dabory')->exists($requestFilePath)) {
  17. return false;
  18. }
  19. $request = json_decode(Storage::disk('dabory')->get($requestFilePath), true);
  20. // 3. api 호출
  21. $response = app(CallApiService::class)->callApi([
  22. 'url' => $url,
  23. 'data' => $request
  24. ]);
  25. // 4. reponse에 json 저장
  26. Storage::disk('dabory')->put($responseFilePath, json_encode($response));
  27. return $response;
  28. }
  29. private function getFullFilePath($filePath, $type): string
  30. {
  31. // TODO: 나중에 세션에서 읽어와서 언어별로 가능하게 수정
  32. $theme = env('DBR_THEME');
  33. return "themes/$theme/pro/para/ko_KR/{$type}/{$filePath}.json";
  34. }
  35. public function deleteCachedDirectory()
  36. {
  37. $theme = env('DBR_THEME');
  38. foreach (preg_replace('/\s+/', '', explode(',', env('LOCALE_SEQUENCE')) ?? []) as $locale) {
  39. Storage::disk('dabory')->deleteDirectory("themes/$theme/pro/para/$locale/response");
  40. }
  41. }
  42. public function filterWidgetTaxo($data, $taxoCode, $DeviceType = '')
  43. {
  44. return collect($data)->filter(function ($widget) use ($taxoCode, $DeviceType) {
  45. if (empty($DeviceType)) {
  46. return $widget['C1'] === $taxoCode;
  47. }
  48. return $widget['C1'] === $taxoCode && $widget['C2'] === $DeviceType;
  49. })->values()->toArray();
  50. }
  51. public function filterItemTaxo($data, $itemTaxoNo)
  52. {
  53. return collect($data)->filter(function ($widget) use ($itemTaxoNo) {
  54. return $widget['C1'] === $itemTaxoNo;
  55. })->values()->toArray();
  56. }
  57. }