Created
February 9, 2018 09:20
-
-
Save killua99/a2bbd99838e60bf695c3f875db43e656 to your computer and use it in GitHub Desktop.
Phprp/soap-client ClientBuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Client Build SoapClient. | |
* | |
* @package WooCommerceSageX3 | |
*/ | |
namespace WooCommerceSageX3\Resources; | |
use Http\Adapter\Guzzle6\Client as GuzzleClient; | |
use Phpro\SoapClient\ClientBuilder as PhproClientBuilder; | |
use Phpro\SoapClient\ClientFactory; | |
use Phpro\SoapClient\ClientInterface; | |
use Phpro\SoapClient\Middleware\BasicAuthMiddleware; | |
use Phpro\SoapClient\Wsdl\Provider\HttPlugWsdlProvider; | |
use Symfony\Component\EventDispatcher\EventDispatcher; | |
class ClientBuilder { | |
/** | |
* Handler client builder, improve error return. | |
* | |
* @param $class | |
* | |
* @return ClientInterface|\WP_Error | |
*/ | |
protected static function client_builder( $class ) { | |
$wsdl = get_option( 'wc_sagex3_wsdl', '' ); | |
if ( empty( $wsdl ) ) { | |
return new \WP_Error( self::$error_code, esc_html__( 'Your wsdl did not met the requirements.', 'woocommerce-sagex3' ) ); | |
} | |
$user = get_option( 'wc_sagex3_user' ); | |
$password = get_option( 'wc_sagex3_password' ); | |
$opts = [ | |
'http' => [ | |
'user_agent' => 'PHPSoapClient', | |
], | |
]; | |
$context = \stream_context_create( $opts ); | |
$client_factory = new ClientFactory( $class ); | |
$soap_options = [ | |
'soap_version' => SOAP_1_2, | |
'stream_context' => $context, | |
'cache_wsdl' => WSDL_CACHE_NONE, | |
// 'login' => $user, | |
// 'password' => $password, | |
'ssl' => [ | |
'verify_peer' => false, | |
'verify_peer_name' => false, | |
'allow_self_signed' => true, | |
], | |
'trace' => 0, | |
]; | |
$client = GuzzleClient::createWithConfig( [ | |
'verify' => false, | |
] ); | |
$provider = HttPlugWsdlProvider::createForClient( $client ); | |
$provider->addMiddleware( new BasicAuthMiddleware( $user, $password ) ); | |
$client_builder = new PhproClientBuilder( $client_factory, $wsdl, $soap_options ); | |
$client_builder->withEventDispatcher( new EventDispatcher() ); | |
$client_builder->withWsdlProvider( $provider ); | |
try { | |
$client = $client_builder->build(); | |
} catch ( \InvalidArgumentException $exception ) { | |
// translators: %s is the message. | |
$msg = vsprintf( esc_html__( 'Something just simple went wrong. Message: %s', 'woocommerce-sagex3' ), [ | |
$exception->getMessage(), | |
] ); | |
return new \WP_Error( self::$error_code, $msg ); | |
} | |
return $client; | |
} | |
/** | |
* Client SOAP. | |
* | |
* @param object $class Object Class. | |
* | |
* @return ClientInterface | |
* @throws InvalidArgumentException | |
*/ | |
public static function client( $class ) : ClientInterface { | |
$client = self::client_builder( $class ); | |
if ( $client instanceof \WP_Error ) { | |
throw new \InvalidArgumentException( $client->get_error_message( self::$error_code ) ); | |
} | |
return $client; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment