<?php

namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap;


use SoapClient;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 27.08.19
 * Time: 15:47
 * Class MySoapClient
 */
class MySoapClient extends SoapClient
{

    /**
     * @param string $request
     * @param string $location
     * @param string $action
     * @param int $version
     * @return string
     */
    public function __doRequest($request, $location, $action, $version)
    {

        if(stripos($request, 'DistributionListActionRequest') !== false)
        {
            $request = str_replace(array('ns1:DistributionListActionRequest', 'xmlns:ns2="urn:zimbra"'), array('ns2:DistributionListActionRequest', 'xmlns:ns2="urn:zimbraAccount"'), $request);
        }

        $exception = null;

        try {
        
            $response = parent::__doRequest($request, $location, $action, $version);
        
        }
        
        catch (SoapFault $sf) {
        
            //this code was not reached    
        
            $exception = $sf;
        
        }
        
        catch (Exception $e) {
        
            //nor was this code reached either
        
            $exception = $e;
        
        }

        if((isset($this->__soap_fault)) && ($this->__soap_fault != null)) {
        
            //this is where the exception from __doRequest is stored
        
            $exception = $this->__soap_fault;

            logModuleCall(
                'zimbraEmail',
                __FUNCTION__,
                $exception,
                "Debug: response",
                $result
            );
        }
        
        return $response;
    }

    /**
     * @param string $function_name
     * @param array $arguments
     * @param array|null $options
     * @param null $input_headers
     * @param array|null $output_headers
     * @return mixed|void
     */
    public function __soapCall($function_name, array $arguments, array $options = null, $input_headers = null, array &$output_headers = null)
    {
        $response = parent::__soapCall($function_name, $arguments, $options, $input_headers, $output_headers);

        return $response;
    }


}