<?php namespace app\controller; use app\item\BookManager; use app\item\AccountManager; use app\item\TagManager; use app\model\Url; use app\view\ItemView; class BookController { /** * URL入力で検索 * DBになければAPIから取得して返す * book/embed で実行 * * searchAction() と違い supplierName, itemKind の信頼性がないので * (悪意あるユーザーが妙な情報をPOSTしうるので) * new Url($inputUrl) でURL解析し supplierName, itemKind を得る */ public function embedAction(): void { // 検索条件 $requestData = json_decode(file_get_contents('php://input'), true); $inputUrl = isset($requestData['url']) ? $requestData['url'] : null; $isLatestRequest = isset($requestData['is_latest_request']) ? $requestData['is_latest_request'] : null; // URL解析し, 一意のデータを検索 $urlInst = new Url($inputUrl); $className = 'app\\item\\' . ucfirst($urlInst->supplierName) . ucfirst($urlInst->itemKind); // ex 'app\\item\\AmazonBook' $supplierItemInst = new $className(); $stmt = $supplierItemInst->findByUrl($urlInst); $bookInst = $stmt->fetch(); // なければ || 最新の要請があれば if (!$bookInst || $isLatestRequest ) { // サプライヤークラス $className = 'app\\supplier\\' . ucfirst($urlInst->supplierName) . 'ApiManager'; // ex 'app\\supplier\\AmazonApiManager' $supplierApiManagerInst = new $className(); // APIのレスポンス取得, 構造の変換, レスポンス保存 $response = $supplierApiManagerInst->fetchByUrl($urlInst); $supplierResponseId = $supplierApiManagerInst->saveSupplierResponse('embed', $response); $bookInstAry = $supplierApiManagerInst->responseToSupplierItemInstAry($response); // レスポンス変換失敗は false が返る /* ★ここに100~120行目と同じコードを書くことがイヤです... */ } $view = new ItemView(); $view->renderInst($bookInst); } /** * ワード入力で検索 * DBになければAPIから取得して返す * book/search で実行 * * フロントからワード入力で検索実行すると * まず checkSearchedWithin24() が実行され * is_searched_within24 がフロントに返るのでそれをPOSTに含めて * 当メソッドが実行される * is_searched_within24 = true ならDBから取得してくる * is_searched_within24 = false ならAPIから検索してくる */ public function searchAction(): void { // 検索条件 $requestData = json_decode(file_get_contents('php://input'), true); $searchWords = isset($requestData['search_words']) ? $requestData['search_words'] : null; $supplierName = isset($requestData['supplier_name']) ? $requestData['supplier_name'] : null; $itemKind = isset($requestData['item_kind']) ? $requestData['item_kind'] : null; $offset = isset($requestData['offset']) ? $requestData['offset'] : null; $isSearchedWithin24 = isset($requestData['is_searched_within24']) ? $requestData['is_searched_within24'] : null; // 24時間以内に検索しているか否かで分岐 if ($isSearchedWithin24) { // 検索条件にあった本群を取得 $conditions = ['search_words'=>$searchWords, 'supplier_name'=>$supplierName, 'item_kind'=>$itemKind, 'offset'=>$offset]; $bookManagerInst = new BookManager(); $resultBookInstAry = $bookManagerInst->findByWords($conditions); } else { // サプライヤークラス $apiConditions = ['search_words'=>$searchWords, 'item_kind'=>$itemKind, 'offset'=>$offset]; $className = 'app\\supplier\\' . ucfirst($supplierName) . 'ApiManager'; // ex 'app\\supplier\\AmazonApiManager' $supplierApiManagerInst = new $className(); // APIのレスポンス取得, 構造の変換, レスポンス保存 $response = $supplierApiManagerInst->fetchByWords($apiConditions); $supplierResponseId = $supplierApiManagerInst->saveSupplierResponse('search', $response); $bookInstAry = $supplierApiManagerInst->responseToSupplierItemInstAry($response); // レスポンス変換失敗は false が返る // DBに保存 $resultBookInstAry = []; if ($bookInstAry) { foreach($bookInstAry as $bookInst) { // book $bookInst->setSupplierResponseId($supplierResponseId); $bookManagerInst = new BookManager(); $bookManagerInst->findOrInsert($bookInst); // accounts $accountManagerInst = new AccountManager(); foreach($bookInst->getAccountInstAry() as $accountInst) { $accountInst->setSupplierResponseId($supplierResponseId); $accountManagerInst->findOrInsert($accountInst); $accountInst->addAccountInstAry($accountInst); } $bookManagerInst->relateBookAccounts($bookInst->getId(), $bookInst->getAccountInstAry()); // tags $tagManagerInst = new TagManager(); foreach($bookInst->getTagInstAry() as $tagInst) { $tagManagerInst->findOrInsert($tagInst); $bookInst->addTagInstAry($tagInst); } $bookManagerInst->relateBookTags($bookInst->getId(), $bookInst->getTagInstAry()); // 返り値にセット $resultBookInstAry[] = $bookInst; } } } $view = new ItemView(); $view->renderInstAry($resultBookInstAry); } }
You have javascript disabled. You will not be able to edit any code.