PageRenderTime 85ms CodeModel.GetById 19ms app.highlight 6ms RepoModel.GetById 57ms app.codeStats 0ms

/api/BatchBuilder.php

https://github.com/octolab/Ecwid
PHP | 208 lines | 111 code | 14 blank | 83 comment | 10 complexity | 7b6bc43d7f3162bbb5d3323dfec0b4ee MD5 | raw file
  1<?php
  2/**
  3 * @author Samigullin Kamil <feedback@kamilsk.com>
  4 * @link http://www.kamilsk.com/
  5 */
  6namespace Ecwid\api;
  7use Ecwid\interfaces\iBatchBuilder,
  8	Ecwid\interfaces\iProductAPI,
  9	\Exception;
 10/**
 11 * @package Ecwid.api
 12 * @since 1.0
 13 */
 14class BatchBuilder implements iBatchBuilder
 15{
 16	/**
 17	 * @var iProductAPI
 18	 */
 19	protected $_product_api;
 20	/**
 21	 * @var array
 22	 */
 23	protected $_queries = array();
 24
 25	/**
 26	 * Constructor.
 27	 *
 28	 * @param iProductAPI $product_api
 29	 */
 30	public function __construct(iProductAPI $product_api)
 31	{
 32		$this->_product_api = $product_api;
 33	}
 34
 35	/* iBatchBuilder */
 36
 37	/**
 38	 * @return iProductAPI
 39	 */
 40	public function getProductAPI()
 41	{
 42		return $this->_product_api;
 43	}
 44
 45	/**
 46	 * @param iProductAPI $product_api
 47	 * @return self
 48	 */
 49	public function setProductAPI(iProductAPI $product_api)
 50	{
 51		$this->_product_api = $product_api;
 52		return $this;
 53	}
 54
 55	/**
 56	 * @param string $alias
 57	 * @param int|null $parent
 58	 * @return self
 59	 * @throws Exception
 60	 */
 61	public function getCategories($alias, $parent = null)
 62	{
 63		$query = 'categories';
 64		if ( ! is_null($parent)) {
 65			if ( ! is_int($parent) or $parent < 0) {
 66				throw new Exception('_invalid_type');
 67			}
 68			$query .= "?parent={$parent}";
 69		}
 70		$this->_addQuery($alias, $query);
 71		return $this;
 72	}
 73
 74	/**
 75	 * @param string $alias
 76	 * @param int $id
 77	 * @return self
 78	 * @throws Exception
 79	 */
 80	public function getCategory($alias, $id)
 81	{
 82		if ( ! is_int($id) or $id < 1) {
 83			throw new Exception('_invalid_type');
 84		}
 85		$query = "category?id={$id}";
 86		$this->_addQuery($alias, $query);
 87		return $this;
 88	}
 89
 90	/**
 91	 * @param string $alias
 92	 * @param int|null $category
 93	 * @return self
 94	 * @throws Exception
 95	 */
 96	public function getProducts($alias, $category = null)
 97	{
 98		$query = 'products';
 99		if ( ! is_null($category)) {
100			if ( ! is_int($category) or $category < 0) {
101				throw new Exception('_invalid_type');
102			}
103			$query .= "?category={$category}";
104		}
105		$this->_addQuery($alias, $query);
106		return $this;
107	}
108
109	/**
110	 * @param string $alias
111	 * @param int $id
112	 * @return self
113	 * @throws Exception
114	 */
115	public function getProduct($alias, $id)
116	{
117		if ( ! is_int($id) or $id < 1) {
118			throw new Exception('_invalid_type');
119		}
120		$query = "product?id={$id}";
121		$this->_addQuery($alias, $query);
122		return $this;
123	}
124
125	/**
126	 * @param string $alias
127	 * @param int $count
128	 * @return self
129	 * @throws Exception
130	 */
131	public function getRandomProducts($alias, $count)
132	{
133		if ( ! is_int($count) or $count < 0) {
134			throw new Exception('_invalid_type');
135		}
136		$query = "random_products?count={$count}";
137		$this->_addQuery($alias, $query);
138		return $this;
139	}
140
141	/**
142	 * @param string $alias
143	 * @return self
144	 * @throws Exception
145	 */
146	public function getClasses($alias)
147	{
148		$query = 'classes';
149		$this->_addQuery($alias, $query);
150		return $this;
151	}
152
153	/**
154	 * @param string $alias
155	 * @param int $id
156	 * @return self
157	 * @throws Exception
158	 */
159	public function getClass($alias, $id)
160	{
161		if ( ! is_int($id) or $id < 0) {
162			throw new Exception('_invalid_type');
163		}
164		$query = "classes?id={$id}";
165		$this->_addQuery($alias, $query);
166		return $this;
167	}
168
169	/**
170	 * @param string $alias
171	 * @return self
172	 * @throws Exception
173	 */
174	public function getProfile($alias)
175	{
176		$query = 'profile';
177		$this->_addQuery($alias, $query);
178		return $this;
179	}
180
181	/**
182	 * @return mixed
183	 * @throws Exception
184	 */
185	public function run()
186	{
187		$result = $this->_product_api->runBatch($this->_queries);
188		$this->_queries = array();
189		return $result;
190	}
191
192	/**
193	 * @param string $alias
194	 * @param string $query
195	 * @return void
196	 * @throws Exception
197	 */
198	protected function _addQuery($alias, $query)
199	{
200		if ( ! is_string($alias)) {
201			throw new Exception('_invalid_type');
202		}
203		if (isset($this->_queries[$alias])) {
204			throw new Exception('_alias_exists');
205		}
206		$this->_queries[$alias] = urlencode($query);
207	}
208}