Subscribe.class.php 3.57 KB
<?php
/**
 * 应用订阅的数据存取类
 * 
 * @name Service_Subscribe
 * @package service
 * @copyright yoho.inc
 * @version 5.0 (2014-02-08 11:02:56)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class Service_Subscribe extends Lib_Service
{
    const ROUTER = 'subscribe.yh_uuc.subscription';
    
    const EXPIRE = 10800; // 缓存三小时
    
    private static $_key = 'key_yh_uuc_tbl_app_info_';
    
    /**
     * 添加订阅
     * 
     * @param string $appId  应用的ID
     * @param string $appSecret  应用的密钥
     * @param string $name  应用名称
     * @param string $scope  订阅信息
     * @param integer $state  应用状态(0:禁用,1:有效)
     * @return boolean (false:失败, true:成功)
     */
    public function add($appId, $appSecret, $name, $scope, $state)
    {
        $result = false;
        
        if ($appId && $appSecret && is_string($name) && is_string($scope) && is_numeric($state))
        {
            $key = sprintf(self::$_key . '%s', $appId);
            $param = array(
                'key' => $appId, 'secret' => $appSecret, 'name' => $name, 
                'scope' => $scope, 'state' => $state, 'create_time' => time(), 
            );
            $result = self::service(self::ROUTER)->key($key)->insert('add', $param)->status();
        }
        
        return $result;
    }
    
    /**
     * 获取订阅
     * 
     * @param string $appId  应用的ID
     * @return array
     */
    public function get($appId)
    {
        $result = array();
        
        if ($appId)
        {
            $key = sprintf(self::$_key . '%s', $appId);
            $param = array('key' => $appId);
            $result = self::service(self::ROUTER)->key($key)->expire(self::EXPIRE)->fetchRow('get', $param);
        }
        
        return $result;
    }
    
    /**
     * 获取所有应用
     * 
     * @return array
     */
    public function getAllApps()
    {
        $result = array();
        $key = self::$_key . '_allApps';
        $result = self::service(self::ROUTER)->key($key)->expire(self::EXPIRE)->fetchAll('getAllApps');
        return $result;
    }
    
    /**
     * 编辑订阅
     * 
     * @param integer $appId  应用的ID
     * @param string $newSecret  新的密钥<optional>
     * @param string $name  应用名称<optional>
     * @param string $scope  订阅信息<optional>
     * @param integer $state  应用状态<optional> (0:表示禁用,1:表示有效)
     * @return boolean  (false:失败, true:成功)
     */
    public function upd($appId, $newSecret, $name, $scope, $state)
    {
        $result = false;
        
        if ($appId)
        {
            $key = sprintf(self::$_key . '%s', $appId);
            $param = array('key' => $appId);
            $appInfo = self::service(self::ROUTER)->key($key)->expire(self::EXPIRE)->fetchRow('get', $param);
            if ($appInfo && isset($appInfo['id']))
            {
                $param = array();
                empty($newSecret) ? true : $param['secret'] = $newSecret;
                empty($name) ? true : $param['name'] = $name;
                empty($scope) ? true : $param['scope'] = $scope;
                is_numeric($state) ? $param['state'] = $state : true;
                if (array() !== $param)
                {
                    $replace = array('STRINGS' => Util_Utils_SqlString::mergeSqlUpstring($param));
                    $param['id'] = $appInfo['id'];
                    $result = self::service(self::ROUTER)->key($key)->update('upd', $param, $replace)->status();
                }
            }
        }
        
        return $result;
    }
}