Subscription.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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-2010 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. /**
  27. * @category Zend
  28. * @package Zend_Feed_Pubsubhubbub
  29. * @subpackage Entity
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Feed_Pubsubhubbub_Model_Subscription
  34. extends Zend_Feed_Pubsubhubbub_Model_ModelAbstract
  35. implements Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
  36. {
  37. /**
  38. * Save subscription to RDMBS
  39. *
  40. * @param array $data
  41. * @return bool
  42. */
  43. public function setSubscription(array $data)
  44. {
  45. if (!isset($data['id'])) {
  46. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  47. throw new Zend_Feed_Pubsubhubbub_Exception(
  48. 'ID must be set before attempting a save'
  49. );
  50. }
  51. $result = $this->_db->find($data['id']);
  52. if ($result) {
  53. $data['created_time'] = $result->current()->created_time;
  54. $now = new Zend_Date;
  55. if ($data['lease_seconds']) {
  56. $data['expiration_time'] = $now->add($data['lease_seconds'], Zend_Date::SECOND)
  57. ->get('yyyy-MM-dd HH:mm:ss');
  58. }
  59. $this->_db->update(
  60. $data,
  61. $this->_db->getAdapter()->quoteInto('id = ?', $data['id'])
  62. );
  63. return false;
  64. }
  65. $this->_db->insert($data);
  66. return true;
  67. }
  68. /**
  69. * Get subscription by ID/key
  70. *
  71. * @param string $key
  72. * @return array
  73. */
  74. public function getSubscription($key)
  75. {
  76. if (empty($key) || !is_string($key)) {
  77. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  78. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
  79. .' of "' . $key . '" must be a non-empty string');
  80. }
  81. $result = $this->_db->find($key);
  82. if ($result) {
  83. return (array) $result->current();
  84. }
  85. return false;
  86. }
  87. /**
  88. * Determine if a subscription matching the key exists
  89. *
  90. * @param string $key
  91. * @return bool
  92. */
  93. public function hasSubscription($key)
  94. {
  95. if (empty($key) || !is_string($key)) {
  96. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  97. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
  98. .' of "' . $key . '" must be a non-empty string');
  99. }
  100. $result = $this->_db->find($key);
  101. if ($result) {
  102. return true;
  103. }
  104. return false;
  105. }
  106. /**
  107. * Delete a subscription
  108. *
  109. * @param string $key
  110. * @return bool
  111. */
  112. public function deleteSubscription($key)
  113. {
  114. $result = $this->_db->find($key);
  115. if ($result) {
  116. $this->_db->delete(
  117. $this->_db->getAdapter()->quoteInto('id = ?', $key)
  118. );
  119. return true;
  120. }
  121. return false;
  122. }
  123. }