/api/ProductAPI.php
PHP | 665 lines | 448 code | 34 blank | 183 comment | 55 complexity | 51146419b0bfb0b9c6e20df75f71b3e3 MD5 | raw file
- <?php
- /**
- * @author Samigullin Kamil <feedback@kamilsk.com>
- * @link http://www.kamilsk.com/
- */
- namespace Ecwid\api;
- use Ecwid\interfaces\iProductAPI,
- \Exception;
- /**
- * @package Ecwid.api
- * @since 1.0
- */
- class ProductAPI implements iProductAPI
- {
- /**
- * @var array
- */
- protected static $_defaultConfig = array(
- 'protocol' => 'http',
- 'base_url' => 'app.ecwid.com/api',
- 'api_version' => 1,
- 'store_id' => 1003,
- 'secure_auth_key' => null,
- );
- /**
- * @var array
- */
- protected $_config;
- /**
- * @var bool
- */
- protected $_silent = false;
- /**
- * @var string
- */
- protected $_url;
- /**
- * @var resource
- */
- protected $_curl;
- /**
- * Constructor.
- *
- * @param array $config
- * <code>
- * array(
- * 'protocol' => %s:http|https,
- * 'base_url' => %s:url,
- * 'api_version' => %i:version,
- * 'store_id' => %i:id,
- * 'secure_auth_key' => %s:key,
- * )
- * </code>
- * @throws Exception
- */
- public function __construct(array $config = array())
- {
- if ($this->_initConfig(array_merge(self::$_defaultConfig, $config))) {
- $this->_updateUrl();
- } else {
- throw new Exception('_init');
- }
- }
- /* iEcwid */
- /**
- * @return string
- */
- public function getProtocol()
- {
- return $this->_config['protocol'];
- }
- /**
- * @param string $protocol
- * @return self
- * @throws Exception
- */
- public function setProtocol($protocol)
- {
- if ($this->_validate('protocol', $protocol)) {
- $this->_config['protocol'] = $protocol;
- $this->_updateUrl();
- return $this;
- }
- throw new Exception('_invalid_protocol');
- }
- /**
- * @return string
- */
- public function getBaseUrl()
- {
- return $this->_config['base_url'];
- }
- /**
- * @param string $base_url
- * @return self
- * @throws Exception
- */
- public function setBaseUrl($base_url)
- {
- if ($this->_validate('base_url', $base_url)) {
- $this->_config['base_url'] = $base_url;
- $this->_updateUrl();
- return $this;
- }
- throw new Exception('_invalid_base_url');
- }
- /**
- * @return int
- */
- public function getApiVersion()
- {
- return $this->_config['api_version'];
- }
- /**
- * @param int $version
- * @return self
- * @throws Exception
- */
- public function setApiVersion($version)
- {
- if ($this->_validate('api_version', $version)) {
- $this->_config['api_version'] = $version;
- $this->_updateUrl();
- return $this;
- }
- throw new Exception('_invalid_api_version');
- }
- /**
- * @return int
- */
- public function getStoreId()
- {
- return $this->_config['store_id'];
- }
- /**
- * @param int $id
- * @return self
- * @throws Exception
- */
- public function setStoreId($id)
- {
- if ($this->_validate('store_id', $id)) {
- $this->_config['store_id'] = $id;
- $this->_updateUrl();
- return $this;
- }
- throw new Exception('_invalid_store_id');
- }
- /**
- * @return string
- */
- public function getUrl()
- {
- return $this->_url;
- }
- /**
- * @return string
- */
- public function getSecureAuthKey()
- {
- return $this->_config['secure_auth_key'];
- }
- /**
- * @param string $key
- * @return self
- * @throws Exception
- */
- public function setSecureAuthKey($key)
- {
- if ($this->_validate('secure_auth_key', $key)) {
- $this->_config['secure_auth_key'] = $key;
- return $this;
- }
- throw new Exception('_invalid_secure_auth_key');
- }
- /**
- * @return array
- */
- public function getConfig()
- {
- return $this->_config;
- }
- /**
- * @param array $config
- * @return bool
- * @throws Exception
- */
- protected function _initConfig(array $config)
- {
- $map = array(
- 'store_id' => 'setStoreId',
- 'protocol' => 'setProtocol',
- 'base_url' => 'setBaseUrl',
- 'api_version' => 'setApiVersion',
- 'secure_auth_key' => 'setSecureAuthKey',
- );
- $this->_silent = true;
- foreach ($config as $key => $value) {
- if (array_key_exists($key, $map)) {
- $set = $map[$key];
- $this->$set($value);
- }
- }
- $this->_silent = false;
- return true;
- }
- /**
- * @param string $key
- * @param mixed $value
- * @return bool
- */
- protected function _validate($key, $value)
- {
- $checksum = 0;
- switch ($key) {
- case 'store_id':
- if (is_int($value)) {
- $checksum += 1;
- }
- if ($value > 0) {
- $checksum += 2;
- }
- return ($checksum === 3);
- case 'protocol':
- if (is_string($value)) {
- $checksum += 1;
- }
- if (preg_match('/^http(s)?$/', $value)) {
- $checksum += 2;
- }
- return ($checksum === 3);
- case 'base_url':
- if (is_string($value)) {
- $checksum += 1;
- }
- return ($checksum === 1);
- case 'api_version':
- if (is_int($value)) {
- $checksum += 1;
- }
- if ($value > 0) {
- $checksum += 2;
- }
- return ($checksum === 3);
- case 'secure_auth_key':
- if (is_string($value) or is_null($value)) {
- $checksum += 1;
- }
- return ($checksum === 1);
- }
- return false;
- }
- /**
- * @return bool
- */
- protected function _updateUrl()
- {
- if ( ! $this->_silent) {
- $parts = array(
- "{$this->getProtocol()}:/",
- $this->getBaseUrl(),
- "v{$this->getApiVersion()}",
- $this->getStoreId(),
- );
- $this->_url = implode('/', $parts);
- }
- return true;
- }
- /* iProductAPI */
- /**
- * @param int|null $parent
- * @return string
- * @throws Exception
- */
- public function getCategories($parent = null)
- {
- $url = $this->getUrl();
- $url .= '/categories';
- if ( ! is_null($parent)) {
- if ( ! is_int($parent) or $parent < 0) {
- throw new Exception('_invalid_type');
- }
- $url .= "?parent={$parent}";
- }
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- ));
- }
- /**
- * @param int $id
- * @return string
- * @throws Exception
- */
- public function getCategory($id)
- {
- if ( ! is_int($id) or $id < 1) {
- throw new Exception('_invalid_type');
- }
- $url = $this->getUrl();
- $url .= "/category?id={$id}";
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- ));
- }
- /**
- * @param int|null $category
- * @return string
- * @throws Exception
- */
- public function getProducts($category = null)
- {
- $url = $this->getUrl();
- $url .= '/products';
- if ( ! is_null($category)) {
- if ( ! is_int($category) or $category < 0) {
- throw new Exception('_invalid_type');
- }
- $url .= "?category={$category}";
- }
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- ));
- }
- /**
- * @param int $id
- * @return string
- * @throws Exception
- */
- public function getProduct($id)
- {
- if ( ! is_int($id) or $id < 1) {
- throw new Exception('_invalid_type');
- }
- $url = $this->getUrl();
- $url .= "/product?id={$id}";
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- ));
- }
- /**
- * @param string $data
- * @return string
- * @throws Exception
- */
- public function putProducts($data)
- {
- if ( ! is_string($data) or empty($data)) {
- throw new Exception('_invalid_type');
- }
- $file = 'put.json';
- $url = $this->getUrl();
- $url .= "/products?secure_auth_key={$this->getSecureAuthKey()}";
- if (false === ($handle = @fopen($file, 'w+'))) {
- throw new Exception('_file_opening');
- }
- if (false === ($length = @fwrite($handle, $data))) {
- throw new Exception('_file_writing');
- }
- if (fseek($handle, 0) === -1) {
- throw new Exception('_file_pointing');
- }
- $result = $this->_fetch(array(
- CURLOPT_URL => $url,
- CURLOPT_PUT => 1,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: text/json',
- ),
- CURLOPT_INFILE => $handle,
- CURLOPT_INFILESIZE => $length,
- ));
- if ( ! fclose($handle)) {
- throw new Exception('_file_closing');
- }
- if ( ! @unlink($file)) {
- throw new Exception('_file_deleting');
- }
- return $result;
- }
- /**
- * @param int $id
- * @param string $data
- * @return string
- * @throws Exception
- */
- public function putProduct($id, $data)
- {
- if (( ! is_int($id) or $id < 1) or ( ! is_string($data) or empty($data))) {
- throw new Exception('_invalid_type');
- }
- $file = 'put.json';
- $url = $this->getUrl();
- $url .= '/product';
- $url .= "?id={$id}";
- $url .= "&secure_auth_key={$this->getSecureAuthKey()}";
- if (false === ($handle = @fopen($file, 'w+'))) {
- throw new Exception('_file_opening');
- }
- if (false === ($length = @fwrite($handle, $data))) {
- throw new Exception('_file_writing');
- }
- if (fseek($handle, 0) === -1) {
- throw new Exception('_file_pointing');
- }
- $result = $this->_fetch(array(
- CURLOPT_URL => $url,
- CURLOPT_PUT => 1,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: text/json',
- ),
- CURLOPT_INFILE => $handle,
- CURLOPT_INFILESIZE => $length,
- ));
- if ( ! fclose($handle)) {
- throw new Exception('_file_closing');
- }
- if ( ! @unlink($file)) {
- throw new Exception('_file_deleting');
- }
- return $result;
- }
- /**
- * @param int $count
- * @return string
- * @throws Exception
- */
- public function getRandomProducts($count)
- {
- if ( ! is_int($count) or $count < 1) {
- throw new Exception('_invalid_type');
- }
- $url = $this->getUrl();
- $url .= "/random_products?count={$count}";
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- ));
- }
- /**
- * @return string
- * @throws Exception
- */
- public function getClasses()
- {
- $url = $this->getUrl();
- $url .= '/classes';
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- ));
- }
- /**
- * @param int $id
- * @return string
- * @throws Exception
- */
- public function getClass($id)
- {
- if ( ! is_int($id) or $id < 0) {
- throw new Exception('_invalid_type');
- }
- $url = $this->getUrl();
- $url .= "/classes?id={$id}";
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- ));
- }
- /**
- * @param string $data
- * @return string
- * @throws Exception
- */
- public function postClass($data)
- {
- if ( ! is_string($data) or empty($data)) {
- throw new Exception('_invalid_type');
- }
- $url = $this->getUrl();
- $url .= "/classes?secure_auth_key={$this->getSecureAuthKey()}";
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- CURLOPT_POST => 1,
- CURLOPT_POSTFIELDS => $data,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: text/json',
- ),
- ));
- }
- /**
- * @param int $id
- * @param string $data
- * @return string
- * @throws Exception
- */
- public function putClass($id, $data)
- {
- if (( ! is_int($id) or $id < 1) or ( ! is_string($data) or empty($data))) {
- throw new Exception('_invalid_type');
- }
- $file = 'put.json';
- $url = $this->getUrl();
- $url .= '/classes';
- $url .= "?id={$id}";
- $url .= "&secure_auth_key={$this->getSecureAuthKey()}";
- if (false === ($handle = @fopen($file, 'w+'))) {
- throw new Exception('_file_opening');
- }
- if (false === ($length = @fwrite($handle, $data))) {
- throw new Exception('_file_writing');
- }
- if (fseek($handle, 0) === -1) {
- throw new Exception('_file_pointing');
- }
- $result = $this->_fetch(array(
- CURLOPT_URL => $url,
- CURLOPT_PUT => 1,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: text/json',
- ),
- CURLOPT_INFILE => $handle,
- CURLOPT_INFILESIZE => $length,
- ));
- if ( ! fclose($handle)) {
- throw new Exception('_file_closing');
- }
- if ( ! @unlink($file)) {
- throw new Exception('_file_deleting');
- }
- return $result;
- }
- /**
- * @param int $id
- * @return string
- * @throws Exception
- */
- public function deleteClass($id)
- {
- if ( ! is_int($id) or $id < 1) {
- throw new Exception('_invalid_type');
- }
- $url = $this->getUrl();
- $url .= '/classes';
- $url .= "?id={$id}";
- $url .= "&secure_auth_key={$this->getSecureAuthKey()}";
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- CURLOPT_CUSTOMREQUEST => 'DELETE',
- ));
- }
- /**
- * @return string
- * @throws Exception
- */
- public function getProfile()
- {
- $url = $this->getUrl();
- $url .= '/profile';
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- ));
- }
- /**
- * @param array $data
- * <code>
- * array(
- * %s:alias => %s:query,
- * ...
- * ),
- * </code>
- * @return string
- * @throws Exception
- */
- public function runBatch(array $data)
- {
- if (empty($data)) {
- throw new Exception('_empty_batch');
- }
- $url = $this->getUrl();
- $url .= '/batch?';
- while (current($data)) {
- $url .= key($data);
- $url .= '=';
- $url .= current($data);
- if (next($data)) {
- $url .= '&';
- }
- }
- return $this->_fetch(array(
- CURLOPT_URL => $url,
- ));
- }
- /**
- * @return bool
- */
- protected function _initCurl()
- {
- $this->_curl = curl_init();
- if ($this->_curl) {
- $config = array(
- CURLOPT_HEADER => 0,
- CURLOPT_RETURNTRANSFER => 1,
- );
- return curl_setopt_array($this->_curl, $config);
- }
- return false;
- }
- /**
- * @param array $config
- * @return string
- * @throws Exception
- */
- protected function _fetch(array $config = array())
- {
- if ( ! $this->_curl) {
- if ( ! $this->_initCurl()) {
- throw new Exception('_curl_undefined');
- }
- }
- if (curl_setopt_array($this->_curl, $config)) {
- $data = curl_exec($this->_curl);
- if ($data === false) {
- $code = curl_errno($this->_curl);
- $data = curl_error($this->_curl);
- throw new Exception($data, $code);
- }
- $code = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE);
- if ($code !== 200) {
- throw new Exception($data, $code);
- }
- return $data;
- }
- throw new Exception('_curl_config');
- }
- }