File indexing completed on 2024-06-23 05:55:13

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Feed_Pubsubhubbub
0017  * @subpackage Entity
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** @see Zend_Feed_Pubsubhubbub_Model_ModelAbstract */
0024 // require_once 'Zend/Feed/Pubsubhubbub/Model/ModelAbstract.php';
0025 
0026 /** @see Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface */
0027 // require_once 'Zend/Feed/Pubsubhubbub/Model/SubscriptionInterface.php';
0028 
0029 /** @see Zend_Date */
0030 // require_once 'Zend/Date.php';
0031 
0032 /**
0033  * @category   Zend
0034  * @package    Zend_Feed_Pubsubhubbub
0035  * @subpackage Entity
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Feed_Pubsubhubbub_Model_Subscription
0040     extends Zend_Feed_Pubsubhubbub_Model_ModelAbstract
0041     implements Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
0042 {
0043 
0044     /**
0045      * Save subscription to RDMBS
0046      *
0047      * @param  array $data
0048      * @throws Zend_Db_Table_Exception
0049      * @throws Zend_Feed_Pubsubhubbub_Exception
0050      * @return bool
0051      */
0052     public function setSubscription(array $data)
0053     {
0054         if (!isset($data['id'])) {
0055             // require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
0056             throw new Zend_Feed_Pubsubhubbub_Exception(
0057                 'ID must be set before attempting a save'
0058             );
0059         }
0060         $result = $this->_db->find($data['id']);
0061         if (count($result)) {
0062             $data['created_time'] = $result->current()->created_time;
0063             $now = new Zend_Date;
0064             if (isset($data['lease_seconds'])) {
0065                 $data['expiration_time'] = $now->add($data['lease_seconds'], Zend_Date::SECOND)
0066                 ->get('yyyy-MM-dd HH:mm:ss');
0067             }
0068             $this->_db->update(
0069                 $data,
0070                 $this->_db->getAdapter()->quoteInto('id = ?', $data['id'])
0071             );
0072             return false;
0073         }
0074 
0075         $this->_db->insert($data);
0076         return true;
0077     }
0078 
0079     /**
0080      * Get subscription by ID/key
0081      *
0082      * @param  string $key
0083      * @throws Zend_Db_Table_Exception
0084      * @throws Zend_Feed_Pubsubhubbub_Exception
0085      * @return array
0086      */
0087     public function getSubscription($key)
0088     {
0089         if (empty($key) || !is_string($key)) {
0090             // require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
0091             throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
0092                 .' of "' . $key . '" must be a non-empty string');
0093         }
0094         $result = $this->_db->find($key);
0095         if (count($result)) {
0096             return $result->current()->toArray();
0097         }
0098         return false;
0099     }
0100 
0101     /**
0102      * Determine if a subscription matching the key exists
0103      *
0104      * @param  string $key
0105      * @throws Zend_Db_Table_Exception
0106      * @throws Zend_Feed_Pubsubhubbub_Exception
0107      * @return bool
0108      */
0109     public function hasSubscription($key)
0110     {
0111         if (empty($key) || !is_string($key)) {
0112             // require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
0113             throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
0114                 .' of "' . $key . '" must be a non-empty string');
0115         }
0116         $result = $this->_db->find($key);
0117         if (count($result)) {
0118             return true;
0119         }
0120         return false;
0121     }
0122 
0123     /**
0124      * Delete a subscription
0125      *
0126      * @param  string $key
0127      * @return bool
0128      */
0129     public function deleteSubscription($key)
0130     {
0131         $result = $this->_db->find($key);
0132         if (count($result)) {
0133             $this->_db->delete(
0134                 $this->_db->getAdapter()->quoteInto('id = ?', $key)
0135             );
0136             return true;
0137         }
0138         return false;
0139     }
0140 
0141 }