BxDolQueue.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolQueue extends BxDolFactory
  10. {
  11. protected $_oQuery;
  12. protected $_sParamTime;
  13. protected $_iExecutionLiveTime;
  14. protected $_iLimitSend;
  15. protected $_iLimitSendPerRecipient;
  16. protected $_aSentTo;
  17. protected function __construct()
  18. {
  19. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  20. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  21. parent::__construct();
  22. $this->_sParamTime = '';
  23. $this->_iExecutionLiveTime = 86400;
  24. }
  25. /**
  26. * Prevent cloning the instance
  27. */
  28. public function __clone()
  29. {
  30. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  31. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  32. }
  33. public function getBusy()
  34. {
  35. return (int)getParam($this->_sParamTime);
  36. }
  37. public function setBusy($iTime = 0)
  38. {
  39. setParam($this->_sParamTime, $iTime);
  40. }
  41. public function isBusy()
  42. {
  43. $iNow = time();
  44. $iTime = $this->getBusy();
  45. $bBusy = !empty($iTime) && ($iNow - $iTime) < $this->_iExecutionLiveTime;
  46. if(!$bBusy)
  47. $this->setBusy($iNow);
  48. return $bBusy;
  49. }
  50. /**
  51. * Send some number of items (email/push) from queue
  52. *
  53. * @param int $iLimit - number of queue items to send
  54. * @return real number of sent queue items or false, if sending process was already started with a previous call and wasn't finished yet.
  55. */
  56. public function send($iLimit = 0)
  57. {
  58. if($this->isBusy())
  59. return false;
  60. $aSent = array();
  61. if(empty($iLimit))
  62. $iLimit = $this->_iLimitSend;
  63. $aItems = $this->_oQuery->getItems(array('type' => 'to_send', 'start' => 0, 'per_page' => $iLimit));
  64. foreach($aItems as $iId => $aItem)
  65. if(call_user_func_array(array($this, '_send'), array_slice($aItem, 1))) {
  66. $this->_oQuery->deleteItem($iId);
  67. $aSent[] = $iId;
  68. }
  69. $this->setBusy();
  70. return count($aSent);
  71. }
  72. }
  73. /** @} */