/api/BatchBuilder.php
PHP | 208 lines | 111 code | 14 blank | 83 comment | 10 complexity | 7b6bc43d7f3162bbb5d3323dfec0b4ee MD5 | raw file
- <?php
- /**
- * @author Samigullin Kamil <feedback@kamilsk.com>
- * @link http://www.kamilsk.com/
- */
- namespace Ecwid\api;
- use Ecwid\interfaces\iBatchBuilder,
- Ecwid\interfaces\iProductAPI,
- \Exception;
- /**
- * @package Ecwid.api
- * @since 1.0
- */
- class BatchBuilder implements iBatchBuilder
- {
- /**
- * @var iProductAPI
- */
- protected $_product_api;
- /**
- * @var array
- */
- protected $_queries = array();
- /**
- * Constructor.
- *
- * @param iProductAPI $product_api
- */
- public function __construct(iProductAPI $product_api)
- {
- $this->_product_api = $product_api;
- }
- /* iBatchBuilder */
- /**
- * @return iProductAPI
- */
- public function getProductAPI()
- {
- return $this->_product_api;
- }
- /**
- * @param iProductAPI $product_api
- * @return self
- */
- public function setProductAPI(iProductAPI $product_api)
- {
- $this->_product_api = $product_api;
- return $this;
- }
- /**
- * @param string $alias
- * @param int|null $parent
- * @return self
- * @throws Exception
- */
- public function getCategories($alias, $parent = null)
- {
- $query = 'categories';
- if ( ! is_null($parent)) {
- if ( ! is_int($parent) or $parent < 0) {
- throw new Exception('_invalid_type');
- }
- $query .= "?parent={$parent}";
- }
- $this->_addQuery($alias, $query);
- return $this;
- }
- /**
- * @param string $alias
- * @param int $id
- * @return self
- * @throws Exception
- */
- public function getCategory($alias, $id)
- {
- if ( ! is_int($id) or $id < 1) {
- throw new Exception('_invalid_type');
- }
- $query = "category?id={$id}";
- $this->_addQuery($alias, $query);
- return $this;
- }
- /**
- * @param string $alias
- * @param int|null $category
- * @return self
- * @throws Exception
- */
- public function getProducts($alias, $category = null)
- {
- $query = 'products';
- if ( ! is_null($category)) {
- if ( ! is_int($category) or $category < 0) {
- throw new Exception('_invalid_type');
- }
- $query .= "?category={$category}";
- }
- $this->_addQuery($alias, $query);
- return $this;
- }
- /**
- * @param string $alias
- * @param int $id
- * @return self
- * @throws Exception
- */
- public function getProduct($alias, $id)
- {
- if ( ! is_int($id) or $id < 1) {
- throw new Exception('_invalid_type');
- }
- $query = "product?id={$id}";
- $this->_addQuery($alias, $query);
- return $this;
- }
- /**
- * @param string $alias
- * @param int $count
- * @return self
- * @throws Exception
- */
- public function getRandomProducts($alias, $count)
- {
- if ( ! is_int($count) or $count < 0) {
- throw new Exception('_invalid_type');
- }
- $query = "random_products?count={$count}";
- $this->_addQuery($alias, $query);
- return $this;
- }
- /**
- * @param string $alias
- * @return self
- * @throws Exception
- */
- public function getClasses($alias)
- {
- $query = 'classes';
- $this->_addQuery($alias, $query);
- return $this;
- }
- /**
- * @param string $alias
- * @param int $id
- * @return self
- * @throws Exception
- */
- public function getClass($alias, $id)
- {
- if ( ! is_int($id) or $id < 0) {
- throw new Exception('_invalid_type');
- }
- $query = "classes?id={$id}";
- $this->_addQuery($alias, $query);
- return $this;
- }
- /**
- * @param string $alias
- * @return self
- * @throws Exception
- */
- public function getProfile($alias)
- {
- $query = 'profile';
- $this->_addQuery($alias, $query);
- return $this;
- }
- /**
- * @return mixed
- * @throws Exception
- */
- public function run()
- {
- $result = $this->_product_api->runBatch($this->_queries);
- $this->_queries = array();
- return $result;
- }
- /**
- * @param string $alias
- * @param string $query
- * @return void
- * @throws Exception
- */
- protected function _addQuery($alias, $query)
- {
- if ( ! is_string($alias)) {
- throw new Exception('_invalid_type');
- }
- if (isset($this->_queries[$alias])) {
- throw new Exception('_alias_exists');
- }
- $this->_queries[$alias] = urlencode($query);
- }
- }