WsdlToPhp / PackageBase

Contains base classes from which the generated classes from PackageGenerator inherit
MIT License
24 stars 26 forks source link

Add support to invoke getLastRequest without throwing an InvalidArgumentException if the request is not executed #22

Closed hordijk closed 6 years ago

hordijk commented 6 years ago

Situation

The invocation to an external webservice is wrapped in a try with a finally block. The finally block invokes $soapClientBase->getLastRequest() to log the request send. If an issue occurred while creating the request (before the request is actually send) invoking $soapClientBase->getLastRequest() results in a InvalidArgumentException due to fact null is supplied to getFormatedXml. Instead of throwing an InvalidArgumentException returning null might be more appropriate.

Possible solution

Support null as input for the getFormatedXml method.

According to the PHPDoc for the public static function getFormatedXml this method can return null as well. In the current implementation it seems it won't return null. It either returns the DOMDocument or the DOMDocument as string. If the supplied string is not valid a InvalidArgumentException is thrown by getDOMDocument.

 /**
  * Returns a XML string content as a DOMDocument or as a formated XML string
  * @throws \InvalidArgumentException
  * @param string $string
  * @param bool $asDomDocument
  * @return \DOMDocument|null
  */
  public static function getFormatedXml($string, $asDomDocument = false)
  {
        $domDocument = self::getDOMDocument($string);
        return $asDomDocument ? $domDocument : $domDocument->saveXML();
  }

The getFormatedXml method is used by getLastXml in AbstractSoapClientBase, as shown in the snippet below:

/**
     * @param string $method
     * @param bool $asDomDocument
     * @return \DOMDocument|string|null
     */
    protected function getLastXml($method, $asDomDocument = false)
    {
        $xml = null;
        if ($this->getSoapClient() instanceof \SoapClient) {
            $xml = static::getFormatedXml($this->getSoapClient()->$method(), $asDomDocument);
        }
        return $xml;
    }

The getLastRequest and getLastResponse methods are using the getLastXml method.

To be able to invoke getLastRequest and getLastResponse in a finally block supporting null seems okay, since this will match the return types (the \DOMDocument|string|null) mentioned in the PHPDoc for getLastRequest and getLastResponse.