diff --git a/src/Artisaninweb/SoapWrapper/Client.php b/src/Artisaninweb/SoapWrapper/Client.php index aa46e8f..8ffd7be 100644 --- a/src/Artisaninweb/SoapWrapper/Client.php +++ b/src/Artisaninweb/SoapWrapper/Client.php @@ -6,186 +6,187 @@ class Client extends SoapClient { - /** - * @var string - */ - protected $wsdl; - - /** - * Client constructor. - * - * @param string $wsdl - * @param array $options - * @param array $headers - */ - public function __construct($wsdl, $options, array $headers = []) - { - parent::SoapClient($wsdl, $options); - - if (!empty($headers)) { - $this->headers($headers); + /** + * @var string + */ + protected $wsdl; + + /** + * Client constructor. + * + * @param string $wsdl + * @param array $options + * @param array $headers + */ + public function __construct($wsdl, $options, array $headers = []) + { + parent::SoapClient($wsdl, $options); + + if (!empty($headers)) { + $this->headers($headers); + } + } + + /** + * Get all functions from the service + * + * @return mixed + */ + public function getFunctions() + { + return $this->__getFunctions(); + } + + /** + * Get the last request + * + * @return mixed + */ + public function getLastRequest() + { + return $this->__getLastRequest(); + } + + /** + * Get the last response + * + * @return mixed + */ + public function getLastResponse() + { + return $this->__getLastResponse(); + } + + /** + * Get the last request headers + * + * @return mixed + */ + public function getLastRequestHeaders() + { + return $this->__getLastRequestHeaders(); + } + + /** + * Get the last response headers + * + * @return mixed + */ + public function getLastResponseHeaders() + { + return $this->__getLastResponseHeaders(); + } + + /** + * Get the types + * + * @return mixed + */ + public function getTypes() + { + return $this->__getTypes(); + } + + /** + * Get all the set cookies + * + * @return mixed + */ + public function getCookies() + { + return $this->__getCookies(); + } + + /** + * Set a new cookie + * + * @param string $name + * @param string $value + * + * @return $this + */ + public function cookie($name, $value) + { + $this->__setCookie($name, $value); + + return $this; + } + + /** + * Set the location + * + * @param string $location + * + * @return $this + */ + public function location($location = '') + { + $this->__setLocation($location); + + return $this; + } + + /** + * Set the Soap headers + * + * @param array $headers + * + * @return $this + */ + protected function headers(array $headers = []) + { + $this->__setSoapHeaders($headers); + + return $this; + } + + /** + * Do soap request + * + * @param string $request + * @param string $location + * @param string $action + * @param string $version + * @param string $one_way + * + * @return mixed + */ + public function doRequest($request, $location, $action, $version, $one_way) + { + return $this->__doRequest($request, $location, $action, $version, $one_way); + } + + /** + * Do a soap call on the webservice client + * + * @param string $function + * @param array $params + * + * @return mixed + */ + public function call($function, $params) + { + return call_user_func_array([$this, $function], $params); + } + + /** + * Allias to do a soap call on the webservice client + * + * @param string $function + * @param array $params + * @param array $options + * @param null $inputHeader + * @param null $outputHeaders + * + * @return mixed + */ + public function SoapCall( + $function, + array $params, + array $options = null, + $inputHeader = null, + &$outputHeaders = null + ) { + return $this->__soapCall($function, $params, $options, $inputHeader, $outputHeaders); } - } - - /** - * Get all functions from the service - * - * @return mixed - */ - public function getFunctions() - { - return $this->__getFunctions(); - } - - /** - * Get the last request - * - * @return mixed - */ - public function getLastRequest() - { - return $this->__getLastRequest(); - } - - /** - * Get the last response - * - * @return mixed - */ - public function getLastResponse() - { - return $this->__getLastResponse(); - } - - /** - * Get the last request headers - * - * @return mixed - */ - public function getLastRequestHeaders() - { - return $this->__getLastRequestHeaders(); - } - - /** - * Get the last response headers - * - * @return mixed - */ - public function getLastResponseHeaders() - { - return $this->__getLastResponseHeaders(); - } - - /** - * Get the types - * - * @return mixed - */ - public function getTypes() - { - return $this->__getTypes(); - } - - /** - * Get all the set cookies - * - * @return mixed - */ - public function getCookies() - { - return $this->__getCookies(); - } - - /** - * Set a new cookie - * - * @param string $name - * @param string $value - * - * @return $this - */ - public function cookie($name, $value) - { - $this->__setCookie($name, $value); - - return $this; - } - - /** - * Set the location - * - * @param string $location - * - * @return $this - */ - public function location($location = '') - { - $this->__setLocation($location); - - return $this; - } - - /** - * Set the Soap headers - * - * @param array $headers - * - * @return $this - */ - protected function headers(array $headers = []) - { - $this->__setSoapHeaders($headers); - - return $this; - } - - /** - * Do soap request - * - * @param string $request - * @param string $location - * @param string $action - * @param string $version - * @param string $one_way - * - * @return mixed - */ - public function doRequest($request, $location, $action, $version, $one_way) - { - return $this->__doRequest($request, $location, $action, $version, $one_way); - } - - /** - * Do a soap call on the webservice client - * - * @param string $function - * @param array $params - * - * @return mixed - */ - public function call($function, $params) - { - return call_user_func_array([$this, $function], $params); - } - - /** - * Allias to do a soap call on the webservice client - * - * @param string $function - * @param array $params - * @param array $options - * @param null $inputHeader - * @param null $outputHeaders - * - * @return mixed - */ - public function SoapCall($function, - array $params, - array $options = null, - $inputHeader = null, - &$outputHeaders = null - ) { - return $this->__soapCall($function, $params, $options, $inputHeader, $outputHeaders); - } } diff --git a/src/Artisaninweb/SoapWrapper/Exceptions/ServiceAlreadyExists.php b/src/Artisaninweb/SoapWrapper/Exceptions/ServiceAlreadyExists.php index 1fb03f0..166c2c5 100644 --- a/src/Artisaninweb/SoapWrapper/Exceptions/ServiceAlreadyExists.php +++ b/src/Artisaninweb/SoapWrapper/Exceptions/ServiceAlreadyExists.php @@ -6,5 +6,5 @@ class ServiceAlreadyExists extends Exception { - // + // } diff --git a/src/Artisaninweb/SoapWrapper/Exceptions/ServiceMethodNotExists.php b/src/Artisaninweb/SoapWrapper/Exceptions/ServiceMethodNotExists.php index 8ea7556..b31e9b0 100644 --- a/src/Artisaninweb/SoapWrapper/Exceptions/ServiceMethodNotExists.php +++ b/src/Artisaninweb/SoapWrapper/Exceptions/ServiceMethodNotExists.php @@ -6,5 +6,5 @@ class ServiceMethodNotExists extends Exception { - // + // } diff --git a/src/Artisaninweb/SoapWrapper/Exceptions/ServiceNotFound.php b/src/Artisaninweb/SoapWrapper/Exceptions/ServiceNotFound.php index 2e78202..1d51ce3 100644 --- a/src/Artisaninweb/SoapWrapper/Exceptions/ServiceNotFound.php +++ b/src/Artisaninweb/SoapWrapper/Exceptions/ServiceNotFound.php @@ -6,5 +6,5 @@ class ServiceNotFound extends Exception { - // + // } diff --git a/src/Artisaninweb/SoapWrapper/Facade.php b/src/Artisaninweb/SoapWrapper/Facade.php index ac54d1e..a409fd3 100644 --- a/src/Artisaninweb/SoapWrapper/Facade.php +++ b/src/Artisaninweb/SoapWrapper/Facade.php @@ -2,13 +2,20 @@ namespace Artisaninweb\SoapWrapper; +/** + * @method static SoapWrapper add(string $name, \Closure $closure) Add a new service to the wrapper + * @method static void addByArray(array $services) Add services by array + * @method static mixed client(string $name, \Closure $closure) Get the client + * @method static mixed call(string $call, array $data, array $options) A easy access call method + * @method static bool has(string $name) Check if wrapper has service + */ class Facade extends \Illuminate\Support\Facades\Facade { - /** - * {@inheritDoc} - */ - protected static function getFacadeAccessor() - { - return SoapWrapper::class; - } + /** + * {@inheritDoc} + */ + protected static function getFacadeAccessor() + { + return SoapWrapper::class; + } } diff --git a/src/Artisaninweb/SoapWrapper/Service.php b/src/Artisaninweb/SoapWrapper/Service.php index 33fa959..e984b0f 100644 --- a/src/Artisaninweb/SoapWrapper/Service.php +++ b/src/Artisaninweb/SoapWrapper/Service.php @@ -7,240 +7,240 @@ class Service { - /** - * @var SoapClient - */ - protected $client; - - /** - * @var string - */ - protected $wsdl; - - /** - * @var boolean - */ - protected $trace; - - /** - * @var array - */ - protected $headers; - - /** - * @var string - */ - protected $cache; - - /** - * @var bool - */ - protected $certificate; - - /** - * @var array - */ - protected $options; - - /** - * @var array - */ - protected $classmap; - - /** - * Service constructor. - */ - public function __construct() - { - $this->wsdl = null; - $this->client = null; - $this->certificate = false; - $this->options = []; - $this->classmap = []; - $this->headers = []; - } - - /** - * Set a custom client - * - * @param SoapClient $client - * - * @return $this - */ - public function client(SoapClient $client) - { - $this->client = $client; - - return $this; - } - - /** - * Get the custom client - * - * @return SoapClient - */ - public function getClient() - { - return $this->client; - } - - /** - * Set the wsdl of the service - * - * @param string $wsdl - * - * @return $this - */ - public function wsdl($wsdl) - { - $this->wsdl = $wsdl; - - return $this; - } - - /** - * Get the wsdl from the service - * - * @return string - */ - public function getWsdl() - { - return $this->wsdl; - } - - /** - * Set trace option - enables tracing of request - * - * @param boolean $trace - * - * @return $this - */ - public function trace($trace) - { - $this->trace = $trace; - - return $this; - } - - /** - * Get the trace option - * - * @return boolean - */ - public function getTrace() - { - return $this->trace; - } - - /** - * Set the WSDL cache - * - * @param $cache - * - * @return $this - */ - public function cache($cache) - { - $this->cache = $cache; - - return $this; - } - - /** - * Get the WSDL cache - * - * @return string - */ - public function getCache() - { - return $this->cache; - } - - /** - * @param array $classmap - * - * @return $this - */ - public function classMap(array $classmap) - { - $this->classmap = $classmap; - - return $this; - } - - /** - * Get the classmap - * - * @return array - */ - public function getClassmap() - { - $classmap = $this->classmap; - $classes = [] ; - - if (!empty($classmap)) { - foreach ($classmap as $class) { - // Can't use end because of strict mode :( - $name = current(array_slice(explode('\\', $class), -1, 1, true)); - - $classes[$name] = $class; - } + /** + * @var SoapClient + */ + protected $client; + + /** + * @var string + */ + protected $wsdl; + + /** + * @var boolean + */ + protected $trace; + + /** + * @var array + */ + protected $headers; + + /** + * @var string + */ + protected $cache; + + /** + * @var bool + */ + protected $certificate; + + /** + * @var array + */ + protected $options; + + /** + * @var array + */ + protected $classmap; + + /** + * Service constructor. + */ + public function __construct() + { + $this->wsdl = null; + $this->client = null; + $this->certificate = false; + $this->options = []; + $this->classmap = []; + $this->headers = []; + } + + /** + * Set a custom client + * + * @param SoapClient $client + * + * @return $this + */ + public function client(SoapClient $client) + { + $this->client = $client; + + return $this; + } + + /** + * Get the custom client + * + * @return SoapClient + */ + public function getClient() + { + return $this->client; + } + + /** + * Set the wsdl of the service + * + * @param string $wsdl + * + * @return $this + */ + public function wsdl($wsdl) + { + $this->wsdl = $wsdl; + + return $this; + } + + /** + * Get the wsdl from the service + * + * @return string + */ + public function getWsdl() + { + return $this->wsdl; + } + + /** + * Set trace option - enables tracing of request + * + * @param boolean $trace + * + * @return $this + */ + public function trace($trace) + { + $this->trace = $trace; + + return $this; + } + + /** + * Get the trace option + * + * @return boolean + */ + public function getTrace() + { + return $this->trace; } - return $classes; - } - - /** - * Set the extra options on the SoapClient - * - * @param array $options - * - * @return $this - */ - public function options(array $options) - { - $this->options = $options; - - return $this; - } - - /** - * Get the extra options - * - * @return array - */ - public function getOptions() - { - $options = [ - 'trace' => $this->getTrace(), - 'cache_wsdl' => $this->getCache(), - 'classmap' => $this->getClassmap(), - ]; - - if ($this->certificate) { - $options['local_cert'] = $this->certificate; + /** + * Set the WSDL cache + * + * @param $cache + * + * @return $this + */ + public function cache($cache) + { + $this->cache = $cache; + + return $this; } - $this->options = array_merge($options, $this->options); - - return $this->options; - } - - /** - * Set the certificate location - * - * @param string $certificate - * - * @return $this - */ - public function certificate($certificate) - { - if ($certificate) { - $this->certificate = $certificate; + /** + * Get the WSDL cache + * + * @return string + */ + public function getCache() + { + return $this->cache; + } + + /** + * @param array $classmap + * + * @return $this + */ + public function classMap(array $classmap) + { + $this->classmap = $classmap; + + return $this; } - return $this; - } + /** + * Get the classmap + * + * @return array + */ + public function getClassmap() + { + $classmap = $this->classmap; + $classes = [] ; + + if (!empty($classmap)) { + foreach ($classmap as $class) { + // Can't use end because of strict mode :( + $name = current(array_slice(explode('\\', $class), -1, 1, true)); + + $classes[$name] = $class; + } + } + + return $classes; + } + + /** + * Set the extra options on the SoapClient + * + * @param array $options + * + * @return $this + */ + public function options(array $options) + { + $this->options = $options; + + return $this; + } + + /** + * Get the extra options + * + * @return array + */ + public function getOptions() + { + $options = [ + 'trace' => $this->getTrace(), + 'cache_wsdl' => $this->getCache(), + 'classmap' => $this->getClassmap(), + ]; + + if ($this->certificate) { + $options['local_cert'] = $this->certificate; + } + + $this->options = array_merge($options, $this->options); + + return $this->options; + } + + /** + * Set the certificate location + * + * @param string $certificate + * + * @return $this + */ + public function certificate($certificate) + { + if ($certificate) { + $this->certificate = $certificate; + } + + return $this; + } /** * Get the headers @@ -252,39 +252,39 @@ public function getHeaders() return $this->headers; } - /** - * Create a new SoapHeader - * - * @param string $namespace - * @param string $name - * @param null $data - * @param bool $mustUnderstand - * @param null $actor - * - * @return $this - */ - public function header($namespace, $name, $data = null, $mustUnderstand = false, $actor = null) - { - if ($actor) { - $this->headers[] = new SoapHeader($namespace, $name, $data, $mustUnderstand, $actor); - } else { - $this->headers[] = new SoapHeader($namespace, $name, $data, $mustUnderstand); + /** + * Create a new SoapHeader + * + * @param string $namespace + * @param string $name + * @param null $data + * @param bool $mustUnderstand + * @param null $actor + * + * @return $this + */ + public function header($namespace, $name, $data = null, $mustUnderstand = false, $actor = null) + { + if ($actor) { + $this->headers[] = new SoapHeader($namespace, $name, $data, $mustUnderstand, $actor); + } else { + $this->headers[] = new SoapHeader($namespace, $name, $data, $mustUnderstand); + } + + return $this; } - return $this; - } - - /** - * Set the Soap headers - * - * @param SoapHeader $header - * - * @return $this - */ - public function customHeader($header) - { - $this->headers[] = $header; - - return $this; - } + /** + * Set the Soap headers + * + * @param SoapHeader $header + * + * @return $this + */ + public function customHeader($header) + { + $this->headers[] = $header; + + return $this; + } } diff --git a/src/Artisaninweb/SoapWrapper/ServiceProvider.php b/src/Artisaninweb/SoapWrapper/ServiceProvider.php index bbaf49a..991d873 100644 --- a/src/Artisaninweb/SoapWrapper/ServiceProvider.php +++ b/src/Artisaninweb/SoapWrapper/ServiceProvider.php @@ -6,31 +6,31 @@ class ServiceProvider extends LaravelServiceProvider { - /** - * Bootstrap the application events. - * - * @return void - */ - public function boot() - { - // Nothing here - } + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + // Nothing here + } - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $soapWrapper = new SoapWrapper(); + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $soapWrapper = new SoapWrapper(); - if (is_array($this->app['config']['soapwrapper'])) { - $soapWrapper->addByArray($this->app['config']['soapwrapper']); - } + if (is_array($this->app['config']['soapwrapper'])) { + $soapWrapper->addByArray($this->app['config']['soapwrapper']); + } - $this->app->bindIf(SoapWrapper::class, function () use ($soapWrapper) { - return $soapWrapper; - }); - } + $this->app->bindIf(SoapWrapper::class, function () use ($soapWrapper) { + return $soapWrapper; + }); + } } diff --git a/src/Artisaninweb/SoapWrapper/SoapWrapper.php b/src/Artisaninweb/SoapWrapper/SoapWrapper.php index 9c611e1..d489a2d 100644 --- a/src/Artisaninweb/SoapWrapper/SoapWrapper.php +++ b/src/Artisaninweb/SoapWrapper/SoapWrapper.php @@ -3,115 +3,112 @@ namespace Artisaninweb\SoapWrapper; use Closure; -use SoapClient; use Artisaninweb\SoapWrapper\Exceptions\ServiceNotFound; use Artisaninweb\SoapWrapper\Exceptions\ServiceAlreadyExists; use Artisaninweb\SoapWrapper\Exceptions\ServiceMethodNotExists; class SoapWrapper { - /** - * @var array - */ - protected $services; - - /** - * SoapWrapper constructor - */ - public function __construct() - { - $this->services = []; - } - - /** - * Add a new service to the wrapper - * - * @param string $name - * @param Closure $closure - * - * @return $this - * @throws ServiceAlreadyExists - */ - public function add($name, Closure $closure) - { - if (!$this->has($name)) { - $service = new Service(); - - $closure($service); - - $this->services[$name] = $service; - - return $this; + /** + * @var array + */ + protected $services; + + /** + * SoapWrapper constructor + */ + public function __construct() + { + $this->services = []; } - throw new ServiceAlreadyExists("Service '" . $name . "' already exists."); - } - - /** - * Add services by array - * - * @param array $services - * - * @return $this - * - * @throws ServiceAlreadyExists - * @throws ServiceMethodNotExists - */ - public function addByArray(array $services = []) - { - if (!empty($services)) { - foreach ($services as $name => $methods) { + /** + * Add a new service to the wrapper + * + * @param string $name + * @param Closure $closure + * + * @return $this + * @throws ServiceAlreadyExists + */ + public function add($name, Closure $closure) + { if (!$this->has($name)) { - $service = new Service(); - - foreach ($methods as $method => $value) { - if (method_exists($service, $method)) { - $service->{$method}($value); - } else { - throw new ServiceMethodNotExists(sprintf( - "Method '%s' does not exists on the %s service.", - $method, - $name - )); - } - } + $service = new Service(); - $this->services[$name] = $service; + $closure($service); - continue; + $this->services[$name] = $service; + + return $this; + } + + throw new ServiceAlreadyExists("Service '" . $name . "' already exists."); + } + + /** + * Add services by array + * + * @param array $services + * + * @return $this + * + * @throws ServiceAlreadyExists + * @throws ServiceMethodNotExists + */ + public function addByArray(array $services = []) + { + if (!empty($services)) { + foreach ($services as $name => $methods) { + if (!$this->has($name)) { + $service = new Service(); + + foreach ($methods as $method => $value) { + if (!method_exists($service, $method)) { + throw new ServiceMethodNotExists(sprintf( + "Method '%s' does not exists on the %s service.", + $method, + $name + )); + } + $service->{$method}($value); + } + + $this->services[$name] = $service; + + continue; + } + + throw new ServiceAlreadyExists(sprintf( + "Service '%s' already exists.", + $name + )); + } } - throw new ServiceAlreadyExists(sprintf( - "Service '%s' already exists.", - $name - )); - } + return $this; } - return $this; - } - - /** - * Get the client - * - * @param string $name - * @param Closure $closure - * - * @return mixed - * @throws ServiceNotFound - */ + /** + * Get the client + * + * @param string $name + * @param Closure $closure + * + * @return mixed + * @throws ServiceNotFound + */ public function client($name, Closure $closure = null) { if ($this->has($name)) { /** @var Service $service */ $service = $this->services[$name]; - if (is_null($service->getClient())) { + $client = $service->getClient(); + + if (is_null($client)) { $client = new Client($service->getWsdl(), $service->getOptions(), $service->getHeaders()); - $service->client($client); - } else { - $client = $service->getClient(); } return $closure($client); @@ -120,33 +117,34 @@ public function client($name, Closure $closure = null) throw new ServiceNotFound("Service '" . $name . "' not found."); } - /** - * A easy access call method - * - * @param string $call - * @param array $data - * - * @return mixed - */ - public function call($call, $data = [], $options = []) - { - list($name, $function) = explode('.', $call, 2); - - return $this->client($name, function ($client) use ($function, $data, $options) { - /** @var Client $client */ - return $client->SoapCall($function, $data, $options); - }); - } - - /** - * Check if wrapper has service - * - * @param string $name - * - * @return bool - */ - public function has($name) - { - return (array_key_exists($name, $this->services)); - } + /** + * A easy access call method + * + * @param string $call + * @param array $data = [] + * @param array $options = [] + * + * @return mixed + */ + public function call($call, $data = [], $options = []) + { + list($name, $function) = explode('.', $call, 2); + + return $this->client($name, function ($client) use ($function, $data, $options) { + /** @var Client $client */ + return $client->soapCall($function, $data, $options); + }); + } + + /** + * Check if wrapper has service + * + * @param string $name + * + * @return bool + */ + public function has($name) + { + return (array_key_exists($name, $this->services)); + } }