PageRenderTime 58ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/classes/XLite/Model/Repo/FormId.php

https://github.com/inhale/core
PHP | 279 lines | 99 code | 27 blank | 153 comment | 12 complexity | 7731df6e1d5ad91af4f483f7816dbeed MD5 | raw file
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * @category LiteCommerce
  17. * @package XLite
  18. * @subpackage Model
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @version GIT: $Id: ffc629a906cf257f7995c51ed1a1de93d91a03b8 $
  23. * @link http://www.litecommerce.com/
  24. * @see ____file_see____
  25. * @since 3.0.0
  26. */
  27. namespace XLite\Model\Repo;
  28. /**
  29. * Form id repository
  30. *
  31. * @package XLite
  32. * @see ____class_see____
  33. * @since 3.0.0
  34. */
  35. class FormId extends \XLite\Model\Repo\ARepo
  36. {
  37. /**
  38. * Repository type
  39. *
  40. * @var string
  41. * @access protected
  42. * @see ____var_see____
  43. * @since 3.0.0
  44. */
  45. protected $type = self::TYPE_SERVICE;
  46. /**
  47. * Form id length
  48. */
  49. const FORM_ID_LENGTH = 32;
  50. /**
  51. * Default 'order by' field name
  52. *
  53. * @var string
  54. * @access protected
  55. * @see ____var_see____
  56. * @since 3.0.0
  57. */
  58. protected $defaultOrderBy = array(
  59. 'date' => false,
  60. 'id' => false,
  61. );
  62. /**
  63. * Form id characters list
  64. *
  65. * @var array
  66. * @access protected
  67. * @see ____var_see____
  68. * @since 3.0.0
  69. */
  70. protected $chars = array(
  71. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  72. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  73. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  74. 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
  75. 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  76. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  77. 'Y', 'Z',
  78. );
  79. /**
  80. * Frontier length
  81. *
  82. * @var integer
  83. * @access protected
  84. * @see ____var_see____
  85. * @since 3.0.0
  86. */
  87. protected $frontierLength = 100;
  88. /**
  89. * Count session by public session id
  90. *
  91. * @param string $formId Form id
  92. * @param integer $sessionId Session id OPTIONAL
  93. *
  94. * @return integer
  95. * @access public
  96. * @see ____func_see____
  97. * @since 3.0.0
  98. */
  99. public function countByFormIdAndSessionId($formId, $sessionId = null)
  100. {
  101. if (!isset($sessionId)) {
  102. $sessionId = \XLite\Core\Session::getInstance()->getModel()->getId();
  103. }
  104. return intval($this->defineByFormIdAndSessionIdQuery($formId, $sessionId)->getSingleScalarResult());
  105. }
  106. /**
  107. * Define query for countByFormIdAndSessionId) method
  108. *
  109. * @param string $formId Form id
  110. * @param integer $sessionId Session id
  111. *
  112. * @return \Doctrine\ORM\QueryBuilder
  113. * @access protected
  114. * @see ____func_see____
  115. * @since 3.0.0
  116. */
  117. protected function defineByFormIdAndSessionIdQuery($formId, $sessionId)
  118. {
  119. return $this->createQueryBuilder('f')
  120. ->select('COUNT(f)')
  121. ->andWhere('f.session_id = :sid AND f.form_id = :fid')
  122. ->setParameter('sid', $sessionId)
  123. ->setParameter('fid', $formId);
  124. }
  125. /**
  126. * Generate public session id
  127. *
  128. * @param integer $sessionId Session id OPTIONAL
  129. *
  130. * @return string
  131. * @access public
  132. * @see ____func_see____
  133. * @since 3.0.0
  134. */
  135. public function generateFormId($sessionId = null)
  136. {
  137. if (!isset($sessionId)) {
  138. $sessionId = \XLite\Core\Session::getInstance()->getModel()->getId();
  139. }
  140. $iterationLimit = 10;
  141. $limit = count($this->chars) - 1;
  142. do {
  143. mt_srand(microtime(true) * 1000);
  144. $id = '';
  145. for ($i = 0; self::FORM_ID_LENGTH > $i; $i++) {
  146. $id .= $this->chars[mt_rand(0, $limit)];
  147. }
  148. $iterationLimit--;
  149. } while (0 < $this->countByFormIdAndSessionId($id, $sessionId) && 0 < $iterationLimit);
  150. if (0 == $iterationLimit) {
  151. // TODO - add throw exception
  152. }
  153. return $id;
  154. }
  155. /**
  156. * Remove expired form IDs
  157. *
  158. * @param integer $sessionId Session id OPTIONAL OPTIONAL
  159. *
  160. * @return void
  161. * @access public
  162. * @see ____func_see____
  163. * @since 3.0.0
  164. */
  165. public function removeExpired($sessionId = null)
  166. {
  167. if (!isset($sessionId)) {
  168. $sessionId = \XLite\Core\Session::getInstance()->getModel()->getId();
  169. }
  170. $id = $this->getFrontierId($sessionId);
  171. if ($id) {
  172. $this->defineRemoveExpiredQuery($id, $sessionId)->execute();
  173. }
  174. }
  175. /**
  176. * Get frontier date
  177. *
  178. * @param integer $sessionId Session id
  179. *
  180. * @return integer|void
  181. * @access protected
  182. * @see ____func_see____
  183. * @since 3.0.0
  184. */
  185. protected function getFrontierId($sessionId)
  186. {
  187. return $this->defineGetFrontierQuery($this->frontierLength, $sessionId)->getSingleScalarResult() ?: null;
  188. }
  189. /**
  190. * Define query for getFrontierId() method
  191. *
  192. * @param integer $frontier Frontier length
  193. * @param integer $sessionId Session id
  194. *
  195. * @return \Doctrine\ORM\QueryBuilder
  196. * @access protected
  197. * @see ____func_see____
  198. * @since 3.0.0
  199. */
  200. protected function defineGetFrontierQuery($frontier, $sessionId)
  201. {
  202. return $this->createQueryBuilder('f')
  203. ->select('f.id')
  204. ->andWhere('f.session_id = :sid')
  205. ->setFirstResult($frontier)
  206. ->setMaxResults(1)
  207. ->setParameter('sid', $sessionId);
  208. }
  209. /**
  210. * Define query for removeExpired() method
  211. *
  212. * @param integer $id Frontier id
  213. * @param integer $sessionId Session id
  214. *
  215. * @return \Doctrine\ORM\QueryBuilder
  216. * @access protected
  217. * @see ____func_see____
  218. * @since 3.0.0
  219. */
  220. protected function defineRemoveExpiredQuery($id, $sessionId)
  221. {
  222. return $this->getQueryBuilder()
  223. ->delete($this->_entityName, 'f')
  224. ->andWhere('f.id < :id AND f.session_id = :sid')
  225. ->setParameter('id', $id)
  226. ->setParameter('sid', $sessionId);
  227. }
  228. /**
  229. * Process DB schema
  230. *
  231. * @param array $schema Schema
  232. * @param string $type Schema type
  233. *
  234. * @return array
  235. * @access public
  236. * @see ____func_see____
  237. * @since 3.0.0
  238. */
  239. public function processSchema(array $schema, $type)
  240. {
  241. $schema = parent::processSchema($schema, $type);
  242. if (\XLite\Core\Database::SCHEMA_CREATE == $type) {
  243. $schema[] = 'ALTER TABLE `' . $this->getClassMetadata()->getTableName() . '`'
  244. . ' ADD CONSTRAINT `formid_to_session` FOREIGN KEY `session_id` (`session_id`)'
  245. . ' REFERENCES `' . $this->_em->getClassMetadata('XLite\Model\Session')->getTableName() . '` (`id`)'
  246. . ' ON DELETE CASCADE ON UPDATE CASCADE';
  247. } elseif (\XLite\Core\Database::SCHEMA_UPDATE == $type) {
  248. $schema = preg_grep('/DROP FOREIGN KEY `?formid_to_session`?/Ss', $schema, PREG_GREP_INVERT);
  249. }
  250. return $schema;
  251. }
  252. }