2
0

Subscription.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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-2012 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-2012 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. * @return bool
  44. */
  45. public function setSubscription(array $data)
  46. {
  47. if (!isset($data['id'])) {
  48. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  49. throw new Zend_Feed_Pubsubhubbub_Exception(
  50. 'ID must be set before attempting a save'
  51. );
  52. }
  53. $result = $this->_db->find($data['id']);
  54. if (count($result)) {
  55. $data['created_time'] = $result->current()->created_time;
  56. $now = new Zend_Date;
  57. if (isset($data['lease_seconds'])) {
  58. $data['expiration_time'] = $now->add($data['lease_seconds'], Zend_Date::SECOND)
  59. ->get('yyyy-MM-dd HH:mm:ss');
  60. }
  61. $this->_db->update(
  62. $data,
  63. $this->_db->getAdapter()->quoteInto('id = ?', $data['id'])
  64. );
  65. return false;
  66. }
  67. $this->_db->insert($data);
  68. return true;
  69. }
  70. /**
  71. * Get subscription by ID/key
  72. *
  73. * @param string $key
  74. * @return array
  75. */
  76. public function getSubscription($key)
  77. {
  78. if (empty($key) || !is_string($key)) {
  79. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  80. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
  81. .' of "' . $key . '" must be a non-empty string');
  82. }
  83. $result = $this->_db->find($key);
  84. if (count($result)) {
  85. return $result->current()->toArray();
  86. }
  87. return false;
  88. }
  89. /**
  90. * Determine if a subscription matching the key exists
  91. *
  92. * @param string $key
  93. * @return bool
  94. */
  95. public function hasSubscription($key)
  96. {
  97. if (empty($key) || !is_string($key)) {
  98. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  99. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
  100. .' of "' . $key . '" must be a non-empty string');
  101. }
  102. $result = $this->_db->find($key);
  103. if (count($result)) {
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * Delete a subscription
  110. *
  111. * @param string $key
  112. * @return bool
  113. */
  114. public function deleteSubscription($key)
  115. {
  116. $result = $this->_db->find($key);
  117. if (count($result)) {
  118. $this->_db->delete(
  119. $this->_db->getAdapter()->quoteInto('id = ?', $key)
  120. );
  121. return true;
  122. }
  123. return false;
  124. }
  125. }