ProApiCache.php 2.3 KB

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