public function run() { if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["original_url"])) { $shorturl = new ShortUrl(new Environment(), new DataStore($this->dbconfig())); $vars = []; try { $vars["shorturl"] = $shorturl->getShortUrl($_POST["original_url"]); } catch (InvalidUrlException $e) { $vars["message"] = $e->getMessage(); } catch (UrlInsertFailException $e) { $vars["message"] = $e->getMessage(); } catch (\Exception $e) { $vars["message"] = "エラーが発生しました。"; } $this->send_body(implode(DIRECTORY_SEPARATOR, ["..", "..", "view", "create_shorturl.php"]), $vars); } else { $this->send_body(implode(DIRECTORY_SEPARATOR, ["..", "..", "view", "create_shorturl.php"])); } }
public function run() { $shorturl = new ShortUrl(new Environment(), new DataStore($this->dbconfig())); $hash = isset($_GET["hash"]) ? $_GET["hash"] : null; if (empty($hash)) { $this->send_status(400); $this->send_body(implode(DIRECTORY_SEPARATOR, ["..", "..", "view", "error.php"]), ["header_message" => "不正なアクセス", "message" => "URLが不正です。"]); } else { $vars = []; try { $url = $shorturl->getOriginalUrl($hash); $this->send_header("Location", $url); } catch (InvalidTokenException $e) { $this->send_status(400); $this->send_body(implode(DIRECTORY_SEPARATOR, ["..", "..", "view", "error.php"]), ["header_message" => "不正なアクセス", "message" => "URLが不正です。"]); } catch (OriginalUrlNotFoundException $e) { $this->send_status(404); $this->send_body(implode(DIRECTORY_SEPARATOR, ["..", "..", "view", "404.php"]), ["message" => "このURLは無効です。"]); } catch (\Exception $e) { $this->send_status(500); $this->send_body(implode(DIRECTORY_SEPARATOR, ["..", "..", "view", "500.php"]), ["message" => "エラーが発生しました。"]); } } }
public function test_getShortUrl_fail_notfound() { $this->init_table(); $shorturl = new ShortUrl(new Environment(), new DataStore(static::$dbconfig)); $url = "http://yahoo.co.jp"; $shortedurl = $shorturl->getShortUrl($url); list(, , , $token) = explode("/", $shortedurl); try { $shorturl->getOriginalUrl($token . "a"); $this->fail(); } catch (OriginalUrlNotFoundException $e) { $this->assertTrue(true); } }