Subscription.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed_Pubsubhubbub
  17. * @subpackage Entity
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** @see Zend_Feed_Pubsubhubbub_Model_ModelAbstract */
  23. require_once 'Zend/Feed/Pubsubhubbub/Model/ModelAbstract.php';
  24. /** @see Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface */
  25. require_once 'Zend/Feed/Pubsubhubbub/Model/SubscriptionInterface.php';
  26. /** @see Zend_Date */
  27. require_once 'Zend/Date.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Feed_Pubsubhubbub
  31. * @subpackage Entity
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Feed_Pubsubhubbub_Model_Subscription
  36. extends Zend_Feed_Pubsubhubbub_Model_ModelAbstract
  37. implements Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
  38. {
  39. /**
  40. * Save subscription to RDMBS
  41. *
  42. * @param array $data
  43. * @throws Zend_Db_Table_Exception
  44. * @throws Zend_Feed_Pubsubhubbub_Exception
  45. * @return bool
  46. */
  47. public function setSubscription(array $data)
  48. {
  49. if (!isset($data['id'])) {
  50. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  51. throw new Zend_Feed_Pubsubhubbub_Exception(
  52. 'ID must be set before attempting a save'
  53. );
  54. }
  55. $result = $this->_db->find($data['id']);
  56. if (count($result)) {
  57. $data['created_time'] = $result->current()->created_time;
  58. $now = new Zend_Date;
  59. if (isset($data['lease_seconds'])) {
  60. $data['expiration_time'] = $now->add($data['lease_seconds'], Zend_Date::SECOND)
  61. ->get('yyyy-MM-dd HH:mm:ss');
  62. }
  63. $this->_db->update(
  64. $data,
  65. $this->_db->getAdapter()->quoteInto('id = ?', $data['id'])
  66. );
  67. return false;
  68. }
  69. $this->_db->insert($data);
  70. return true;
  71. }
  72. /**
  73. * Get subscription by ID/key
  74. *
  75. * @param string $key
  76. * @throws Zend_Db_Table_Exception
  77. * @throws Zend_Feed_Pubsubhubbub_Exception
  78. * @return array
  79. */
  80. public function getSubscription($key)
  81. {
  82. if (empty($key) || !is_string($key)) {
  83. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  84. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
  85. .' of "' . $key . '" must be a non-empty string');
  86. }
  87. $result = $this->_db->find($key);
  88. if (count($result)) {
  89. return $result->current()->toArray();
  90. }
  91. return false;
  92. }
  93. /**
  94. * Determine if a subscription matching the key exists
  95. *
  96. * @param string $key
  97. * @throws Zend_Db_Table_Exception
  98. * @throws Zend_Feed_Pubsubhubbub_Exception
  99. * @return bool
  100. */
  101. public function hasSubscription($key)
  102. {
  103. if (empty($key) || !is_string($key)) {
  104. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  105. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
  106. .' of "' . $key . '" must be a non-empty string');
  107. }
  108. $result = $this->_db->find($key);
  109. if (count($result)) {
  110. return true;
  111. }
  112. return false;
  113. }
  114. /**
  115. * Delete a subscription
  116. *
  117. * @param string $key
  118. * @return bool
  119. */
  120. public function deleteSubscription($key)
  121. {
  122. $result = $this->_db->find($key);
  123. if (count($result)) {
  124. $this->_db->delete(
  125. $this->_db->getAdapter()->quoteInto('id = ?', $key)
  126. );
  127. return true;
  128. }
  129. return false;
  130. }
  131. }