<?php
class Common_Etp {
    protected $_env = 'development';
    protected $_scheme = array(
        'development' => 'http',//开发
        'test' => 'http',//测试
        'product' => 'https'//生产
    );
    protected $_apiKey = array(
        'development' => '17F96064551CCFDDE050007F0100760A',//开发
        'test' => '17F96064551CCFDDE050007F0100760A',//测试
        'product' => '17F96064551CCFDDE050007F0100760A'//生产
    );
    //主机名
    protected $_host = array(
        'development' => '192.168.27.13',//开发
        'test' => '120.24.2.78',//测试
        'product' => 'order.etradepay.com'//生产
    );
    //端口
    protected $_port = array(
        'development' => '8065',//开发
        'test' => '8060',//测试
        'product' => '443'//生产
    );
    protected $_service = array(
        'createRecord' => '/rs/settlement/record/create',//发送ETP备案清单
        'sendOrderReference' => '/rs/outbound/order/create',//发送出去单
        'confirmReceivedOrder' => '/rs/order/deliveryOrder',//确认收货
    );

    public function __construct() {
        $this->setEnv();
    }
    //设置环境
    public function setEnv() {
        $config = Zend_Registry::get('config');
        if( $config->env ) {
            $this->_env = $config->env;
        }
    }

    public function getEnv() {
        return $this->_env;
    }

    public function getHost() {
        return isset( $this->_host[ $this->_env ] ) ? $this->_host[ $this->_env ] : '';
    }

    public function setScheme( $scheme ) {
        $this->_scheme = $scheme;
    }

    public function getScheme() {
        return isset( $this->_scheme[ $this->_env ] ) ? $this->_scheme[ $this->_env ] : 'http';
    }

    public function getPort() {
        return isset( $this->_port[ $this->_env ] ) ? $this->_port[ $this->_env ] : '';
    }

    public function getService( $service ) {
        return $service && isset( $this->_service[ $service ] ) ? $this->_service[ $service ] : '';
    }

    public function getApiKey() {
        return isset( $this->_apiKey[ $this->_env ] ) ? $this->_apiKey[ $this->_env ] : '';
    }

    public function getUrl( $service ) {
        if( $service && isset( $this->_service[ $service ] ) ) {
            $service = $this->_service[ $service ];
            $scheme = $this->getScheme();
            $host = $this->getHost();
            $port = $this->getPort();
            return $scheme.'://'.$host.( $port ? ':'.$port : '' ).$service;
        }
        return '';
    }
}