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