Subscribe.class.php 3.4 KB
<?php
/**
 * 应用订阅的操作类
 * 
 * @name Facade_Subscribe
 * @package facade
 * @copyright yoho.inc
 * @version 5.0 (2014-02-08 10:59:47)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class Facade_Subscribe
{
    private static $_service = null;
    
    const APP_KEY_YOHOBUY = '1392109259';
    
    const APP_KEY_SHOW = '1392109210';
    
    /**
     * 单例模式实例化服务层对象
     * 
     * @return object
     */
    private static function service()
    {
        if (null === self::$_service)
        {
            self::$_service = new Service_Subscribe();
        }
        return self::$_service;
    }
    
    /**
     * 添加订阅
     *
     * @param string $appId  应用的ID
     * @param string $appSecret  应用的密钥
     * @param string $name  应用名称
     * @param string $scope  订阅信息(多个之间以逗号“,”分隔开, 格式如nick,gender)
     * @param integer $state  应用状态(0:禁用,1:有效)
     * @return mixed <false:失败, array:成功>
     */
    public static function add($appId, $appSecret, $name, $scope, $state)
    {
        $result = false;
    
        if (is_string($name) && is_string($scope) && is_numeric($state))
        {
            $name = trim($name);
            $scope = trim(trim($scope, ','));
            if ($name !== '' && $scope !== '')
            {
                $appId = $appId ? strval($appId) : time();
                $appSecret = $appSecret ? strval($appSecret) : md5($name.$appId.mt_rand());
                $status = self::service()->add($appId, $appSecret, $name, $scope, $state);
                
                if ($status)
                {
                    $result = self::service()->get($appId);
                }
            }
        }
    
        return $result;
    }
    
    /**
     * 获取订阅
     *
     * @param string $appId  应用的ID
     * @return array
     */
    public static function get($appId)
    {
        $result = array();
    
        if (isset($appId))
        {
            $result = self::service()->get($appId);
        }
    
        return $result;
    }
    

    /**
     * 获取所有APP
     *
     * @return array
     */
    public static function getAllApps()
    {
        $result = self::service()->getAllApps();
        return $result ? $result : array();
    }
    
    /**
     * 编辑订阅
     *
     * @param string $appId  应用的ID
     * @param string $newSecret  颁发密钥<optional> (不修改时为null)
     * @param string $name  应用名称<optional> (不修改时为null)
     * @param string $scope  订阅信息<optional> (不修改时为null)
     * @param integer $state  应用状态<optional> (不修改时为null;0:表示禁用,1:表示有效)
     * @return mixed <false:失败, array:成功>
     */
    public static function upd($appId, $newSecret, $name, $scope, $state)
    {
        $result = false;
    
        if (isset($appId))
        {
            $newSecret = is_string($newSecret) ? trim($newSecret) : null;
            $name = is_string($name) ? trim($name) : null;
            $scope = is_string($scope) ? trim(trim($scope, ',')) : null;
            $state = is_numeric($state) ? intval($state) : null;
            
            $status = self::service()->upd($appId, $newSecret, $name, $scope, $state);
            if ($status)
            {
                $result = self::service()->get($appId);
            }
        }
    
        return $result;
    }
}