MainController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\Http;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Http\Request;
  6. use App\Http\Traits\CommonTrait;
  7. use App\Models\Popup;
  8. use App\Models\Movie\MovieReview;
  9. use App\Models\DTO\SearchData;
  10. use App\Services\BoardService;
  11. class MainController extends Controller
  12. {
  13. use CommonTrait;
  14. private BoardService $boardService;
  15. private Popup $popupModel;
  16. private MovieReview $movieReviewModel;
  17. /**
  18. * Create a new controller instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct(
  23. BoardService $boardService, Popup $popupModel, MovieReview $movieReviewModel
  24. ) {
  25. $this->middleware('front');
  26. $this->boardService = $boardService;
  27. $this->popupModel = $popupModel;
  28. $this->movieReviewModel = $movieReviewModel;
  29. }
  30. /**
  31. * 메인 페이지
  32. * @method GET
  33. * @see /
  34. * @return \Illuminate\Contracts\Support\Renderable
  35. */
  36. public function index(Request $request)
  37. {
  38. // 팝업
  39. $popups = $this->popupModel->list();
  40. // 현재 상영작
  41. $nowPlaying = $this->_nowPlaying();
  42. // 공지사항
  43. $notice = $this->boardService->latest('notice', 1, DEFAULT_LIST_PER_PAGE);
  44. // 평점·후기
  45. $review = $this->_review($request);
  46. // 인기 영화
  47. $trending = $this->_trending();
  48. return view(layout('main'), [
  49. 'popups' => $popups,
  50. 'nowPlaying' => $nowPlaying,
  51. 'notice' => $notice,
  52. 'review' => $review,
  53. 'trending' => $trending
  54. ]);
  55. }
  56. /**
  57. * 현재 상영작
  58. */
  59. private function _nowPlaying(): object
  60. {
  61. return $this->_requestTMDB(TMDB_HOST . TMDB_GET_NOW_PLAYING);
  62. }
  63. /**
  64. * 최근 평점·후기 목록
  65. */
  66. private function _review(Request $request): object
  67. {
  68. $cacheName = 'latest-review';
  69. if (!$latest = Cache::get($cacheName)) {
  70. $params = SearchData::fromRequest($request);
  71. $params->sort = 2;
  72. $latest = $this->movieReviewModel->list($params);
  73. if($latest->total) {
  74. $num = listNum($latest->total, $params->page, $params->perPage);
  75. foreach ($latest->list as $i => $row) {
  76. $row->num = $num--;
  77. $row->viewURL = route('movie.review.show', base64_encode($row->movie_cd));
  78. $row->createdAt = $this->dateFormat($row->created_at, 'Y.m.d');
  79. $latest->list[$i] = $row;
  80. }
  81. $latest->list->filter();
  82. $latest->pagination = null;
  83. Cache::tags('latest-review')->put($cacheName, $latest, CACHE_EXPIRE_TIME);
  84. }
  85. }
  86. return $latest;
  87. }
  88. /**
  89. * 최신 트랜딩
  90. */
  91. private function _trending(): object
  92. {
  93. return $this->_requestTMDB(sprintf(TMDB_HOST . TMDB_TRENDING, 'movie', 'day'));
  94. }
  95. /**
  96. * 최신 트랜딩
  97. */
  98. private function _requestTMDB(string $host): object
  99. {
  100. if(!$ret = Cache::get($host))
  101. {
  102. // 곧 개봉 예정
  103. $response = Http::get($host, [
  104. 'api_key' => TMDB_API_KEY,
  105. 'language' => 'ko',
  106. 'region' => 'kr'
  107. ]);
  108. $list = [];
  109. if($response->ok()) {
  110. foreach(json_decode($response->body())->results as $row) {
  111. $row->posterURL = ('https://image.tmdb.org/t/p/original' . $row->poster_path);
  112. $row->backdropURL = ('https://image.tmdb.org/t/p/original' . $row->backdrop_path);
  113. $row->releaseDate = date('Y.m.d', strtotime($row->release_date));
  114. $row->voteAverage = round($row->vote_average, 2);
  115. $list[] = $row;
  116. }
  117. }
  118. $ret = (object)['total' => count($list), 'list' => $list];
  119. Cache::put($host, $ret, CACHE_EXPIRE_TIME);
  120. }
  121. return $ret;
  122. }
  123. }