From 61a84d71a72bea2d60f08fbef883f394f899bf76 Mon Sep 17 00:00:00 2001 From: inser7 Date: Fri, 17 Jan 2020 18:44:25 +0300 Subject: [PATCH 1/2] improve downloader - check if error --- app/Downloader.php | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/app/Downloader.php b/app/Downloader.php index 077a0e2..82f0c72 100644 --- a/app/Downloader.php +++ b/app/Downloader.php @@ -22,6 +22,38 @@ public function setCacher(CacherInterface $c) { $this->cacher = $c; } + /** + * Fetching data. + * + * @return string + * @throws \Exception + */ + private function fetch(string $method, string $url, string $body="", array $headers = []) + { + $context = stream_context_create([ + "http" => [ + // http://docs.php.net/manual/en/context.http.php + "method" => $method, + "header" => implode("\r\n", $headers), + "content" => $body, + "ignore_errors" => true, + ], + ]); + + $response = file_get_contents($url, false, $context); + $status_line = $http_response_header[0]; + + preg_match('{HTTP\/\S*\s(\d{3})}', $status_line, $match); + + $status = $match[1]; + + if ($status !== "200") { + throw new \Exception("unexpected response status: {$status_line}\n" . $response); + } + + return $response; + } + /** * Downloading data. @@ -45,7 +77,7 @@ function download() { // TODO: check if '200 Ok' here - $content = @file_get_contents($this->url); + $content = $this->fetch("GET", $this->url); if ( ! $content) { throw new \Exception(sprintf('Downloader: No content at %s', $this->url)); From 09a980e7d767a0dd685ed27a053a0e6df872106e Mon Sep 17 00:00:00 2001 From: ins7 Date: Fri, 17 Jan 2020 23:01:15 +0300 Subject: [PATCH 2/2] cleanup the code --- app/Downloader.php | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/app/Downloader.php b/app/Downloader.php index 82f0c72..bdfa6e8 100644 --- a/app/Downloader.php +++ b/app/Downloader.php @@ -2,6 +2,8 @@ namespace App; +use Exception; + /** * The implementation is responsible for getting raw data from specified URL/path. * @@ -11,13 +13,23 @@ class Downloader implements DownloaderInterface { private $url; + /** + * Numeric status code, 200: OK + */ + const HTTP_OK = 200; + + /** + * @param $url + */ public function setUrl($url) { $this->url = $url; } - private $cacher; + /** + * @param CacherInterface $c + */ public function setCacher(CacherInterface $c) { $this->cacher = $c; } @@ -25,14 +37,17 @@ public function setCacher(CacherInterface $c) { /** * Fetching data. * + * @param string $method + * @param string $url + * @param string $body + * @param array $headers * @return string - * @throws \Exception + * @throws Exception */ - private function fetch(string $method, string $url, string $body="", array $headers = []) + private function fetch(string $method, string $url, string $body="", array $headers = []) : string { $context = stream_context_create([ "http" => [ - // http://docs.php.net/manual/en/context.http.php "method" => $method, "header" => implode("\r\n", $headers), "content" => $body, @@ -45,28 +60,27 @@ private function fetch(string $method, string $url, string $body="", array $head preg_match('{HTTP\/\S*\s(\d{3})}', $status_line, $match); - $status = $match[1]; + $status = (int)$match[1]; - if ($status !== "200") { - throw new \Exception("unexpected response status: {$status_line}\n" . $response); + if ($status !== Downloader::HTTP_OK) { + throw new Exception("Unexpected response status: {$status_line}\n" . $response); } return $response; } - /** * Downloading data. * * @return string - * @throws \Exception + * @throws Exception */ - function download() { + function download() : string + { if ( ! $this->url) { - throw new \Exception('Downloader: No url specified'); + throw new Exception('Downloader: No url specified'); } - if ($this->cacher) { $key = sprintf('url_%s', $this->url); @@ -75,20 +89,16 @@ function download() { } } - - // TODO: check if '200 Ok' here $content = $this->fetch("GET", $this->url); if ( ! $content) { - throw new \Exception(sprintf('Downloader: No content at %s', $this->url)); + throw new Exception(sprintf('Downloader: No content at %s', $this->url)); } - if ($this->cacher) { $this->cacher->put($key, $content); } - return $content; } }