Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use JmsfwkCommunicatorDataServiceGetContactSubscription;
- use JmsfwkCommunicatorResourcesCommunicatorCredentials;
- use JmsfwkCommunicatorResourcesSubscriptionInfo;
- use JmsfwkCommunicatorServicesDataService;
- $credentials = new CommunicatorCredentials('username', 'password');
- $dataService = new DataService($credentials);
- $request = new GetContactSubscription('adam@example.com', 12345);
- //try {...
- $subscriptionInfo = $dataService->getContactSubscription($request);
- abstract class Service
- {
- const NS = 'http://ws.communicatorcorp.com/';
- /**
- * @var SoapClient
- */
- protected $client;
- /**
- * @var CommunicatorCredentials
- */
- protected $credentials;
- /**
- * Service constructor.
- *
- * @param CommunicatorCredentials $credentials
- * @param SoapClient $client
- */
- public function __construct(CommunicatorCredentials $credentials,
- SoapClient $client = null)
- {
- $this->credentials = $credentials;
- if ($client === null) {
- $client = self::makeClient();
- }
- $this->client = $client;
- $header = new SoapHeader(self::NS, 'CommunicatorCredentials',
- $credentials->getHeader());
- $this->client->__setSoapHeaders($header);
- }
- public static function makeClient(array $soapSettings = []): SoapClient
- {
- $defaults = [
- 'trace' => false,
- 'exception' => true,
- 'soap_version' => SOAP_1_2,
- ];
- return new SoapClient(static::getWSDL(), array_merge($defaults,
- $soapSettings));
- }
- abstract protected static function getWSDL(): string;
- }
- namespace JmsfwkCommunicatorServices;
- use JmsfwkCommunicatorDataServiceGetContactSubscription;
- use JmsfwkCommunicatorResourcesSubscriptionInfo;
- use SoapClient;
- class DataService extends Service
- {
- const WSDL = 'https://ws.communicatorcorp.com/DataService.asmx?WSDL';
- public function getContactSubscription(
- GetContactSubscription $request): SubscriptionInfo
- {
- $response = $this->client->GetContactSubscription($request);
- return GetContactSubscription::formatResponse(
- $response->GetContactSubscriptionResult);
- }
- public static function makeClient(array $soapSettings = []): SoapClient
- {
- $defaults = [
- 'trace' => true,
- 'exception' => true,
- 'soap_version' => SOAP_1_2,
- ];
- return new SoapClient(self::WSDL, array_merge($defaults, $soapSettings));
- }
- }
- namespace JmsfwkCommunicatorResources;
- use SoapHeader;
- use SoapVar;
- class CommunicatorCredentials extends Resource
- {
- /** Communicator's namespace */
- const NAME_SPACE = 'http://ws.communicatorcorp.com/';
- /** The name of the XML node in the SOAP request */
- const HEADER_NAME = 'CommunicatorCredentials';
- /** @var string */
- public $Username;
- /** @var string */
- public $Password;
- /**
- * @param string $username
- * @param string $password
- */
- public function __construct(string $username, string $password)
- {
- $this->Username = $username;
- $this->Password = $password;
- }
- /**
- * Build a SoapHeader for authenticating with Communicator.
- *
- * Communicator requires that the authentication header is built of SoapVar's
- * instead of an object matching the required fields.
- *
- * @return SoapHeader
- */
- public function getHeader(): SoapHeader
- {
- $data = new SoapVar(
- [
- new SoapVar($this->Username, XSD_STRING, null,
- null, 'Username', self::NAME_SPACE),
- new SoapVar($this->Password, XSD_STRING, null,
- null, 'Password', self::NAME_SPACE),
- ],
- SOAP_ENC_OBJECT
- );
- return new SoapHeader(self::NAME_SPACE, self::HEADER_NAME, $data);
- }
- }
- namespace JmsfwkCommunicatorDataService;
- use JmsfwkCommunicatorExceptionsBadResponseData;
- use JmsfwkCommunicatorResourcesResource;
- use JmsfwkCommunicatorResourcesSubscriptionInfo;
- use TypeError;
- use UnexpectedValueException;
- class GetContactSubscription extends Resource
- {
- protected $emailAddress;
- protected $mailingListId;
- public function __construct(string $emailAddress, int $mailingListId)
- {
- $this->emailAddress = $emailAddress;
- $this->mailingListId = $mailingListId;
- }
- public static function formatResponse(
- stdClass $response): SubscriptionInfo
- {
- try {
- return new SubscriptionInfo(
- $response->MailingListId,
- $response->IsSubscribed,
- new SubscriptionSourceType($response->SubscriptionSourceType),
- DateTime::createFromFormat("Y-m-dTH:i:s",
- $response->DateLastAction)
- );
- } catch (UnexpectedValueException $e) {
- throw new BadResponseData($e->getMessage(), $e->getCode(), $e);
- } catch (TypeError $e) {
- throw new BadResponseData($e->getMessage(), $e->getCode(), $e);
- }
- }
- }
- namespace JmsfwkCommunicatorResources;
- class SubscriptionInfo extends Resource
- {
- public $MailingListId;
- public $IsSubscribed;
- public $DateLastAction;
- public $SubscriptionSourceType = 'TestCase';
- public function __construct($MailingListId, $IsSubscribed = true)
- {
- $this->MailingListId = $MailingListId;
- $this->IsSubscribed = $IsSubscribed;
- $this->DateLastAction = date("Y-m-dTH:i:s");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement