CompanyController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Http\Controllers\Movie;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Client\ConnectionException;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\Validator;
  8. use App\Http\Controllers\Controller;
  9. use App\Http\Traits\CommonTrait;
  10. use App\Http\Traits\PagingTrait;
  11. use App\Models\DTO\SearchData;
  12. use App\Models\DTO\Movie\CompanyParams;
  13. use App\Models\Movie\Company;
  14. use Exception;
  15. class CompanyController extends Controller
  16. {
  17. use CommonTrait;
  18. use PagingTrait;
  19. private string $host;
  20. private Company $companyModel;
  21. public function __construct(Company $companyModel) {
  22. $this->middleware('front');
  23. $this->host = env('MOVIE_API');
  24. $this->companyModel = $companyModel;
  25. }
  26. /**
  27. * 영화사 목록 (DB 조회)
  28. * @method GET
  29. * @see /movie/company
  30. */
  31. public function index(Request $request)
  32. {
  33. $rules = [
  34. 'page' => 'nullable|numeric',
  35. 'keyword' => 'nullable|string|max:100',
  36. 'ceo' => 'nullable|string|max:30'
  37. ];
  38. $attributes = [
  39. 'page' => '페이지 번호',
  40. 'keyword' => '영화사명',
  41. 'ceo' => '대표자명'
  42. ];
  43. $message = [
  44. 'page.numeric' => '페이지 번호는 숫자여야 합니다.',
  45. 'keyword.max' => '영화사명 최대 글자는 100자 입니다.',
  46. 'ceo.max' => '대표자명 최대 글자는 30자 입니다.'
  47. ];
  48. // 검색 유효성 검사
  49. Validator::make($request->all(), $rules, $message, $attributes)->validated();
  50. $params = SearchData::fromRequest($request);
  51. $data = $this->companyModel->list($params);
  52. // 상세 보기 URL 세팅
  53. $queryString = $request->getQueryString();
  54. foreach ($data->list as $row) {
  55. $row->viewURL = route('movie.company.show', base64_encode($row->companyCd));
  56. if ($queryString) {
  57. $row->viewURL .= ('?' . $queryString);
  58. }
  59. }
  60. return view(layout('movie.company.index'), [
  61. 'keyword' => $request->get('keyword'),
  62. 'ceo' => $request->get('ceo'),
  63. 'total' => $data->total,
  64. 'companies' => $data->list
  65. ]);
  66. }
  67. /**
  68. * 영화사 상세 보기 (DB 우선 + 캐시, 미수집 시 API 폴백)
  69. * @method GET
  70. * @see /movie/company/{company_cd}
  71. */
  72. public function show(string $base64, Request $request)
  73. {
  74. try {
  75. if(!$base64) {
  76. throw new Exception('잘못된 접근입니다. [1]');
  77. }
  78. $companyCd = base64_decode($base64);
  79. if(!$companyCd) {
  80. throw new Exception('잘못된 접근입니다. [2]');
  81. }
  82. $listURL = route('movie.company.index');
  83. if($queryString = $request->getQueryString()) {
  84. $listURL .= ('?' . $queryString);
  85. }
  86. // 캐시 → DB → crawler API 폴백 순으로 조회
  87. $cacheKey = ('movie:company:' . $companyCd);
  88. $info = Cache::get($cacheKey);
  89. if (!$info) {
  90. $info = $this->companyModel->info($companyCd);
  91. // DB 미수집이면 crawler API 폴백 (수집은 cron 이 채움)
  92. if (!$info) {
  93. $info = $this->fetchFromApi($companyCd);
  94. }
  95. if ($info && !empty($info->companyCd)) {
  96. Cache::put($cacheKey, $info, CACHE_EXPIRE_TIME);
  97. }
  98. }
  99. if(!$info || empty($info->companyCd)) {
  100. throw new Exception('요청한 정보가 없습니다.');
  101. }
  102. $companyNm = $info->companyNm;
  103. if(!empty($info->companyNmEn)) {
  104. $companyNm .= (' (' . $info->companyNmEn . ')');
  105. }
  106. return view(layout('movie.company.show'), [
  107. 'companyNm' => $companyNm,
  108. 'info' => $info,
  109. 'listURL' => $listURL
  110. ]);
  111. } catch(Exception $e) {
  112. abort($e->getCode() ?: 500, $e->getMessage());
  113. }
  114. }
  115. /**
  116. * DB 미수집 영화사를 crawler API 로 즉석 조회 (폴백)
  117. */
  118. private function fetchFromApi(string $companyCd): ?object
  119. {
  120. try {
  121. $params = new CompanyParams();
  122. $params->companyCd = $companyCd;
  123. $response = Http::timeout(5)->get($this->host . COMPANY_INFO, $params->toArray());
  124. } catch (ConnectionException) {
  125. return null;
  126. }
  127. if (!$response->ok()) {
  128. return null;
  129. }
  130. $data = json_decode($response->body());
  131. return ($data->info ?? null);
  132. }
  133. }