PrototypedSoapClient.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2008 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00041     abstract class PrototypedSoapClient
00042     {
00043         protected $wsdlUrl      = null;
00044         protected $classMap     = array();
00045         
00046         protected $soapClient   = null;
00047         
00048         final public static function convertSoapFault(SoapFault $e)
00049         {
00050             $r = new ReflectionObject($e);
00051             
00052             if (!$r->hasProperty('detail') || !($e->detail instanceof stdClass))
00053                 return $e;
00054             
00055             $r = new ReflectionObject($e->detail);
00056             
00057             if (
00058                 $r->hasProperty('exception')
00059                 && $e->detail->exception instanceof SoapVar
00060             ) {
00061                 $exception = $e->detail->exception->enc_value;
00062                 
00063                 Assert::isInstance($exception, 'BaseException');
00064                 
00065                 return $exception;
00066             }
00067             
00068             return $e;
00069         }
00070         
00071         public function __construct()
00072         {
00073             $wsdlUrl = $this->getWsdlUrl();
00074             
00075             Assert::isNotNull($wsdlUrl);
00076             
00077             $this->soapClient = new SoapClient(
00078                 $wsdlUrl,
00079                 array(
00080                     'soap_version'  => SOAP_1_1,
00081                     'classmap'      => $this->classMap(),
00082                     
00083                     // TODO:?
00084                     /*
00085                     'compression'   => SOAP_COMPRESSION_ACCEPT
00086                         | SOAP_COMPRESSION_GZIP
00087                     */
00088                     
00089                     'trace'         => true,
00090                     'exceptions'    => true
00091                 )
00092             );
00093         }
00094         
00095         public function getWsdlUrl()
00096         {
00097             return $this->wsdlUrl;
00098         }
00099         
00100         public function classMap()
00101         {
00102             return $this->classMap;
00103         }
00104         
00105         protected function call($method, DTOMessage $request, $resultClass)
00106         {
00107             $requestDto = $request->makeDto();
00108             
00109             Assert::isInstance($requestDto, 'DTOClass');
00110             
00111             if (defined('__LOCAL_DEBUG__') && !defined('SIMPLE_TEST') ) {
00112                 // self-validation
00113                 
00114                 $form = ObjectToFormConverter::create($request->entityProto())->
00115                     make($request);
00116                 
00117                 Assert::isTrue(
00118                     !$form->getErrors()
00119                     && $request->entityProto()->
00120                         validate($request, $form),
00121                     
00122                     Assert::dumpArgument($request)
00123                 );
00124             }
00125             
00126             try {
00127                 try {
00128                     
00129                     $resultDto = $this->getSoapClient()->$method($requestDto);
00130                     
00131                 } catch (BaseException $e) {
00132                     
00133                     if (get_class($e) == 'BaseException') {
00134                         throw new SoapFault(
00135                             'Server',
00136                             get_class($e).': '.$e->getMessage()
00137                         );
00138                         
00139                     } else {
00140                         $this->logCall();
00141                         throw $e;
00142                     }
00143                 }
00144                 
00145             } catch (SoapFault $e) {
00146                 
00147                 $this->logCall();
00148                 throw self::convertSoapFault($e);
00149             }
00150             
00151             $this->logCall();
00152             
00153             if (!$resultClass) {
00154                 Assert::isNull($resultDto);
00155                 $result = null;
00156                 
00157             } else {
00158                 Assert::isInstance($resultDto, 'DTOClass');
00159                 
00160                 Assert::isEqual(
00161                     $resultDto->entityProto()->className(),
00162                     $resultClass
00163                 );
00164                 
00165                 $form = DTOToFormImporter::create($resultDto->entityProto())->
00166                     make($resultDto);
00167                 
00168                 Assert::isTrue(
00169                     !$form->getErrors(),
00170                     
00171                     Assert::dumpArgument($resultDto)
00172                 );
00173                 
00174                 $result = $resultDto->makeObject($form);
00175                 
00176                 Assert::isInstance($result, 'DTOMessage');
00177                 
00178                 Assert::isEqual(get_class($result), $resultClass);
00179                 
00180                 Assert::isTrue(
00181                     $result->entityProto()->
00182                         validate($result, $form),
00183                         
00184                     Assert::dumpArgument($result)
00185                 );
00186             }
00187             
00188             return $result;
00189         }
00190         
00191         protected function getLastRequestCdata()
00192         {
00193             return $this->getXmlCdata(
00194                 $this->getSoapClient()->__getLastRequest()
00195             );
00196         }
00197         
00198         protected function getLastResponseCdata()
00199         {
00200             return $this->getXmlCdata(
00201                 $this->getSoapClient()->__getLastResponse()
00202             );
00203         }
00204         
00211         protected function logCall()
00212         {
00213             return $this;
00214         }
00215         
00216         private function getXmlCdata($xml)
00217         {
00218             return "<![CDATA[\n".$xml."\n]]>";
00219         }
00220         
00229         public function getSoapClient()
00230         {
00231             return $this->soapClient;
00232         }
00233         
00234         public function __getLastRequestHeaders()
00235         {
00236             return $this->getSoapClient()->__getLastRequestHeaders();
00237         }
00238         
00239         public function __getLastResponseHeaders()
00240         {
00241             return $this->getSoapClient()->__getLastResponseHeaders();
00242         }
00243         
00244         public function __getLastResponse()
00245         {
00246             return $this->getSoapClient()->__getLastResponse();
00247         }
00248         
00249         public function __getLastRequest()
00250         {
00251             return $this->getSoapClient()->__getLastRequest();
00252         }
00253         
00257     }
00258 ?>