<?php
/**
 * TNT OFFICIAL MODULE FOR PRESTASHOP
 *
 * @author    GFI Informatique <www.gfi.fr>
 * @copyright 2016-2017 GFI Informatique, 2016-2017 TNT
 * @license   https://opensource.org/licenses/MIT MIT License
 */

require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_PasswordManager.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Logger.php';

class TNTOfficiel_SoapClient
{
    protected $_username;
    protected $_password;
    protected $_account;
    protected $_wsdl;
    private $logger;

    /**
     * Constructor : Set Data
     */
    public function __construct()
    {
        TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));

        $this->_username = Configuration::get('TNT_CARRIER_USERNAME');
        $this->_password = TNTOfficiel_PasswordManager::decrypt(Configuration::get('TNT_CARRIER_PASSWORD'));
        $this->_account = Configuration::get('TNT_CARRIER_ACCOUNT');
        $this->_wsdl = Configuration::get('TNT_CARRIER_SOAP_WSDL');

        $this->logger = new TNTOfficiel_Logger();
    }

    /**
     * @param $reference
     * @return bool|array
     */
    public function trackingByConsignment($reference)
    {
        TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));

        try {
            $client = $this->createClient();
            $response = $client->trackingByConsignment(array('parcelNumber' => $reference));
            $this->logger->logMessageTnt('trackingByConsignment', null, 'JSON', true, 'SUCCESS');
        } catch (Exception $objException) {
            $strStatus = ($objException->getCode() == 500) ? 'FATAL' : 'ERROR';
            $strMsg = $objException->getMessage();
            $this->logger->logMessageTnt('trackingByConsignment', $strMsg, 'JSON', false, $strStatus);
            return false;
        }

        return $response;
    }

    /**
     * Return an instance of SoapHeader for WS Security
     *
     * @param string $login
     * @param string $password
     *
     * @return \SoapHeader
     */
    public function getSecurityHeader($login, $password)
    {
        TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));

        $authHeader = sprintf(
            $this->getSecurityHeaderTemplate(), htmlspecialchars($login), htmlspecialchars($password)
        );

        return $authHeader;
    }

    /**
     * Return template for WS Security header
     *
     * @return string
     */
    protected function getSecurityHeaderTemplate()
    {
        TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));

        return '<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                    <wsse:UsernameToken>
                        <wsse:Username>%s</wsse:Username>
                        <wsse:Password>%s</wsse:Password>
                    </wsse:UsernameToken>
                </wsse:Security>';
    }

    /**
     * Return a new instance of SoapClient
     *
     * @return \SoapClient
     */
    public function createClient()
    {
        TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));

        $client = new SoapClient($this->_wsdl, array(
            'soap_version' => SOAP_1_1,
            'trace'        => 1,
        ));
        $authvars = new SoapVar($this->getSecurityHeader($this->_username, $this->_password), XSD_ANYXML);
        $soapHeaders = new SoapHeader("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", $authvars);
        $client->__setSOAPHeaders($soapHeaders);

        return $client;
    }

}