From ac0c0948648cc5c3d1354b4e026e3d3b1f5a4ac8 Mon Sep 17 00:00:00 2001 From: edim24 Date: Wed, 18 Jan 2017 23:01:59 +0300 Subject: [PATCH 1/3] Update webmasterApi.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Использование exception вместо trigger_error --- src/webmasterApi.php | 104 ++++++++----------------------------------- 1 file changed, 19 insertions(+), 85 deletions(-) diff --git a/src/webmasterApi.php b/src/webmasterApi.php index c737ca2..6f692e9 100644 --- a/src/webmasterApi.php +++ b/src/webmasterApi.php @@ -65,26 +65,7 @@ class webmasterApi * * @var int */ - public $userID = null; - - - /** - * Last error message - * - * @var string - */ - public $lastError = ''; - - - /** - * - * User trigger errors - * - * Передавать ли возникающие ошибки в стандартный поток ошибок/ - * - * @var boolean - */ - public $triggerError = true; + protected $userID = null; /** @@ -100,8 +81,7 @@ function __construct($accessToken) $this->accessToken = $accessToken; $this->userID = $this->getUserID(); if (isset($this->userID->error_message)) { - $this->errorCritical($this->userID->error_message); - $this->userID = null; + throw new webmasterException($this->userID->error_message); } } @@ -119,9 +99,6 @@ function __construct($accessToken) static function initApi($accessToken) { $wmApi = new self($accessToken); - if (!empty($wmApi->lastError)) { - return (object)array('error_message' => $wmApi->lastError); - } return $wmApi; } @@ -139,7 +116,6 @@ public function getApiUrl($resource) { $apiurl = $this->apiUrl; if ($resource !== '/user/') { - if (!$this->userID) return $this->errorCritical("Can't get hand " . $resource . " without userID"); $apiurl .= "/user/" . $this->userID; } return $apiurl . $resource; @@ -192,15 +168,15 @@ protected function get($resource, $data = array()) $response = @file_get_contents($url, 0, $context); } else { - return $this->errorCritical('CURL not installed & file_get_contents disabled'); + throw new webmasterException('CURL not installed & file_get_contents disabled'); } - if (!$response) return $this->errorCritical('Error in curl when get [' . $url . '] ' . $curl_error); + if (!$response) throw new webmasterException('Error in curl when get [' . $url . '] ' . $curl_error); $response = json_decode($response, false, 512, JSON_BIGINT_AS_STRING); - if (!is_object($response)) return $this->errorCritical('Unknown error in response: Not object given'); + if (!is_object($response)) throw new webmasterException('Unknown error in response: Not object given'); return $response; } @@ -250,13 +226,13 @@ protected function post($resource, $data) $response = @file_get_contents($url, 0, $context); } else { - return $this->errorCritical('CURL not installed & file_get_contents disabled'); + throw new webmasterException('CURL not installed & file_get_contents disabled'); } - if (!$response) return $this->errorCritical('Unknown error in curl'); + if (!$response) throw new webmasterException('Unknown error in curl'); $response = json_decode($response); - if (!is_object($response)) return $this->errorCritical('Unknown error in curl'); + if (!is_object($response)) throw new webmasterException('Unknown error in curl'); return $response; } @@ -312,13 +288,13 @@ protected function delete($resource, $data = array()) if (in_array('HTTP/1.1 204 No Content', $http_response_header)) return (object)array(true); } else { - return $this->errorCritical('CURL not installed & file_get_contents disabled'); + throw new webmasterException('CURL not installed & file_get_contents disabled'); } - if (!$response) return $this->errorCritical('Unknown error in curl'); + if (!$response) throw new webmasterException('Unknown error in curl'); $response = json_decode($response); - if (!is_object($response)) return $this->errorCritical('Unknown error in curl'); + if (!is_object($response)) throw new webmasterException('Unknown error in curl'); return $response; } @@ -362,52 +338,12 @@ private function dataToString($data) $new_data[] = urlencode($param) . "=" . urlencode($value_item); } } else { - $this->errorWarning("Bad type of key " . $param . ". Value must be string or array"); - continue; + throw new webmasterException("Bad type of key " . $param . ". Value must be string or array"); } } return implode("&", $new_data); } - - /** - * Save error message and return false - * - * @param $message string Text of message - * @param $json boolean return false as json error - * - * @return false|object - */ - private function errorCritical($message, $json = true) - { - $this->lastError = $message; - if ($json) { - if ($this->triggerError) trigger_error($message, E_USER_ERROR); - return (object)array('error_code' => 'CRITICAL_ERROR', 'error_message' => $message); - } - return false; - } - - - /** - * Save and log notice message and return false - * - * @param $message string Text of message - * @param $json boolean return false as json error - * - * @return false|object - */ - private function errorWarning($message, $json = true) - { - $this->lastError = $message; - if ($json) { - if ($this->triggerError) trigger_error($message, E_USER_NOTICE); - return (object)array('error_code' => 'CRITICAL_ERROR', 'error_message' => $message); - } - return false; - } - - /** * Get User ID for current access token * @@ -421,8 +357,10 @@ private function getUserID() $ret = $this->get('/user/'); if (!isset($ret->user_id) || !intval($ret->user_id)) { $mes = "Can't resolve USER ID"; - if (isset($ret->error_message)) $mes .= ". " . $ret->error_message; - return $this->errorCritical($mes); + if (isset($ret->error_message)) { + $mes .= ". " . $ret->error_message; + } + throw new webmasterException($mes); } return $ret->user_id; } @@ -667,9 +605,9 @@ public function getIndexingHistory($hostID, $indexing_indicators = array('DOWNLO { if (!$date_from) $date_from = time() - 1209600; if (!$date_to) $date_to = time(); - if (!intval($date_to) || !$date_to) return $this->errorCritical("Bad timestamp to \$date_to"); - if (!intval($date_from) || !$date_from) return $this->errorCritical("Bad timestamp to \$date_from"); - if ($date_to < $date_from) return $this->errorCritical("Date to can't be smaller then Date from"); + if (!intval($date_to) || !$date_to) throw new webmasterException("Bad timestamp to \$date_to"); + if (!intval($date_from) || !$date_from) throw new webmasterException("Bad timestamp to \$date_from"); + if ($date_to < $date_from) throw new webmasterException("Date to can't be smaller then Date from"); $ret = $this->get('/hosts/' . $hostID . "/indexing-history/", array("indexing_indicator" => $indexing_indicators, 'date_from' => date(DATE_ISO8601, $date_from), 'date_to' => date(DATE_ISO8601, $date_to))); return $ret; @@ -849,7 +787,3 @@ static function getAccessToken($code, $client_id, $client_secret) return $response; } } - - - - From 94d79dacebcc8f7a8221f3384cccb39608ee39e7 Mon Sep 17 00:00:00 2001 From: edim24 Date: Wed, 18 Jan 2017 23:03:40 +0300 Subject: [PATCH 2/3] Add files via upload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Отдельный класс для исключений --- src/webmasterException.php | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/webmasterException.php diff --git a/src/webmasterException.php b/src/webmasterException.php new file mode 100644 index 0000000..8c07c66 --- /dev/null +++ b/src/webmasterException.php @@ -0,0 +1,6 @@ + Date: Fri, 16 Feb 2018 11:29:02 +0300 Subject: [PATCH 3/3] Update webmasterApi.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Возможность указания и получения userID извне. --- src/webmasterApi.php | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/webmasterApi.php b/src/webmasterApi.php index 6f692e9..eadf683 100644 --- a/src/webmasterApi.php +++ b/src/webmasterApi.php @@ -76,14 +76,17 @@ class webmasterApi * * @param $accessToken string access token from Yandex ouath serverh */ - function __construct($accessToken) + function __construct($accessToken, $userId = null) { $this->accessToken = $accessToken; - $this->userID = $this->getUserID(); - if (isset($this->userID->error_message)) { - throw new webmasterException($this->userID->error_message); - } - + if (is_null($userId)) { + $this->userID = $this->getUserID(); + if (isset($this->userID->error_message)) { + throw new webmasterException($this->userID->error_message); + } + } else { + $this->userID = $userId; + } } @@ -352,17 +355,21 @@ private function dataToString($data) * * @return int|false */ - private function getUserID() + public function getUserID() { - $ret = $this->get('/user/'); - if (!isset($ret->user_id) || !intval($ret->user_id)) { - $mes = "Can't resolve USER ID"; - if (isset($ret->error_message)) { - $mes .= ". " . $ret->error_message; - } - throw new webmasterException($mes); + if (is_null($this->userID)) { + $ret = $this->get('/user/'); + if (!isset($ret->user_id) || !intval($ret->user_id)) { + $mes = "Can't resolve USER ID"; + if (isset($ret->error_message)) { + $mes .= ". " . $ret->error_message; + } + throw new webmasterException($mes); + } + return $ret->user_id; + } else { + return $this->userID; } - return $ret->user_id; }