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