<?php

/**
 * Note : Code is released under the GNU LGPL
 *
 * Please do not change the header of this file
 *
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU
 * Lesser General Public License as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU Lesser General Public License for more details.
 */

/**
 * File:        Web.php
 * Project:     DHL API
 *
 * @author      Al-Fallouji Bashar
 * @version     0.1
 */

namespace DHL\Client;
include_once (__DIR__."/../nusoap.php");

use DHL\Entity\Base as Request;

/**
 * DHL Webservice Client
 */
class Web
{
    private $_stagingUrl = 'https://wsbexpress.dhl.com/sndpt/expressRateBook';
    private $_productionUrl = 'https://wsbexpress.dhl.com/gbl/expressRateBook';
    
    private $_stagingUrlTracking = 'https://wsbexpress.dhl.com/sndpt/glDHLExpressTrack';
    private $_productionUrlTracking = 'https://wsbexpress.dhl.com/gbl/glDHLExpressTrack';
    

    protected $_mode = 'staging';
    protected $_type = 'shipping';

    /**
     * Class constructor
     * 
     * @var string $mode Mode can be either production or staging
     */
    public function __construct($mode = 'staging', $type = 'shipping')
    {
        if (!in_array($mode, array('staging', 'production'))) 
        {
            throw new \InvalidArgumentException('Invalid mode : ' . $mode . '. Accepted values are : staging or production.');
        }
        $this->_mode = $mode;
        $this->_type = $type;
    }

    /**
     * Call DHL Service
     * 
     * @param Request $request Request to send
     * 
     * @return string DHL XML response string
     */
    
     public function call($request, $respose_node = 'shipresp:ShipmentResponse')
    {
       
        $curl = curl_init();

        curl_setopt_array($curl, array(
        CURLOPT_URL => $this->_getUrl(),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>$request->toXML(),
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/xml',
            
        ),
        ));

        $response = curl_exec($curl);
        if (curl_error($curl)){
            return curl_error($curl);
        }
        curl_close($curl);
        $parser = new \nusoap_parser($response, 'UTF-8', $respose_node,null);
	    $array =$parser->get_soapbody();
	
        return $array;
    }

    /**
     * Get url associated to a specific service
     * 
     * @return string URL for the service
     */
    private function _getUrl()
    {
        if($this->_type == 'shipping'){
            return ('staging' == $this->_mode) ? $this->_stagingUrl : $this->_productionUrl;
        }
        if($this->_type == 'tracking'){
            
            return ('staging' == $this->_mode) ? $this->_stagingUrlTracking : $this->_productionUrlTracking;
        }
    }
}
