Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. use JmsfwkCommunicatorDataServiceGetContactSubscription;
  2. use JmsfwkCommunicatorResourcesCommunicatorCredentials;
  3. use JmsfwkCommunicatorResourcesSubscriptionInfo;
  4. use JmsfwkCommunicatorServicesDataService;
  5.  
  6. $credentials = new CommunicatorCredentials('username', 'password');
  7. $dataService = new DataService($credentials);
  8. $request = new GetContactSubscription('adam@example.com', 12345);
  9.  
  10. //try {...
  11. $subscriptionInfo = $dataService->getContactSubscription($request);
  12.  
  13. abstract class Service
  14. {
  15. const NS = 'http://ws.communicatorcorp.com/';
  16.  
  17. /**
  18. * @var SoapClient
  19. */
  20. protected $client;
  21.  
  22. /**
  23. * @var CommunicatorCredentials
  24. */
  25. protected $credentials;
  26.  
  27. /**
  28. * Service constructor.
  29. *
  30. * @param CommunicatorCredentials $credentials
  31. * @param SoapClient $client
  32. */
  33. public function __construct(CommunicatorCredentials $credentials,
  34. SoapClient $client = null)
  35. {
  36. $this->credentials = $credentials;
  37.  
  38. if ($client === null) {
  39. $client = self::makeClient();
  40. }
  41. $this->client = $client;
  42.  
  43. $header = new SoapHeader(self::NS, 'CommunicatorCredentials',
  44. $credentials->getHeader());
  45.  
  46. $this->client->__setSoapHeaders($header);
  47. }
  48.  
  49. public static function makeClient(array $soapSettings = []): SoapClient
  50. {
  51. $defaults = [
  52. 'trace' => false,
  53. 'exception' => true,
  54. 'soap_version' => SOAP_1_2,
  55. ];
  56.  
  57. return new SoapClient(static::getWSDL(), array_merge($defaults,
  58. $soapSettings));
  59. }
  60.  
  61. abstract protected static function getWSDL(): string;
  62. }
  63.  
  64. namespace JmsfwkCommunicatorServices;
  65.  
  66. use JmsfwkCommunicatorDataServiceGetContactSubscription;
  67. use JmsfwkCommunicatorResourcesSubscriptionInfo;
  68. use SoapClient;
  69.  
  70. class DataService extends Service
  71. {
  72.  
  73. const WSDL = 'https://ws.communicatorcorp.com/DataService.asmx?WSDL';
  74.  
  75. public function getContactSubscription(
  76. GetContactSubscription $request): SubscriptionInfo
  77. {
  78. $response = $this->client->GetContactSubscription($request);
  79.  
  80. return GetContactSubscription::formatResponse(
  81. $response->GetContactSubscriptionResult);
  82. }
  83.  
  84. public static function makeClient(array $soapSettings = []): SoapClient
  85. {
  86. $defaults = [
  87. 'trace' => true,
  88. 'exception' => true,
  89. 'soap_version' => SOAP_1_2,
  90. ];
  91.  
  92. return new SoapClient(self::WSDL, array_merge($defaults, $soapSettings));
  93. }
  94. }
  95.  
  96. namespace JmsfwkCommunicatorResources;
  97.  
  98. use SoapHeader;
  99. use SoapVar;
  100.  
  101. class CommunicatorCredentials extends Resource
  102. {
  103. /** Communicator's namespace */
  104. const NAME_SPACE = 'http://ws.communicatorcorp.com/';
  105. /** The name of the XML node in the SOAP request */
  106. const HEADER_NAME = 'CommunicatorCredentials';
  107.  
  108. /** @var string */
  109. public $Username;
  110. /** @var string */
  111. public $Password;
  112.  
  113. /**
  114. * @param string $username
  115. * @param string $password
  116. */
  117. public function __construct(string $username, string $password)
  118. {
  119. $this->Username = $username;
  120. $this->Password = $password;
  121. }
  122.  
  123. /**
  124. * Build a SoapHeader for authenticating with Communicator.
  125. *
  126. * Communicator requires that the authentication header is built of SoapVar's
  127. * instead of an object matching the required fields.
  128. *
  129. * @return SoapHeader
  130. */
  131. public function getHeader(): SoapHeader
  132. {
  133. $data = new SoapVar(
  134. [
  135. new SoapVar($this->Username, XSD_STRING, null,
  136. null, 'Username', self::NAME_SPACE),
  137. new SoapVar($this->Password, XSD_STRING, null,
  138. null, 'Password', self::NAME_SPACE),
  139. ],
  140. SOAP_ENC_OBJECT
  141. );
  142.  
  143. return new SoapHeader(self::NAME_SPACE, self::HEADER_NAME, $data);
  144. }
  145. }
  146.  
  147. namespace JmsfwkCommunicatorDataService;
  148.  
  149. use JmsfwkCommunicatorExceptionsBadResponseData;
  150. use JmsfwkCommunicatorResourcesResource;
  151. use JmsfwkCommunicatorResourcesSubscriptionInfo;
  152. use TypeError;
  153. use UnexpectedValueException;
  154.  
  155. class GetContactSubscription extends Resource
  156. {
  157. protected $emailAddress;
  158. protected $mailingListId;
  159.  
  160. public function __construct(string $emailAddress, int $mailingListId)
  161. {
  162. $this->emailAddress = $emailAddress;
  163. $this->mailingListId = $mailingListId;
  164. }
  165.  
  166. public static function formatResponse(
  167. stdClass $response): SubscriptionInfo
  168. {
  169. try {
  170. return new SubscriptionInfo(
  171. $response->MailingListId,
  172. $response->IsSubscribed,
  173. new SubscriptionSourceType($response->SubscriptionSourceType),
  174. DateTime::createFromFormat("Y-m-dTH:i:s",
  175. $response->DateLastAction)
  176. );
  177. } catch (UnexpectedValueException $e) {
  178. throw new BadResponseData($e->getMessage(), $e->getCode(), $e);
  179. } catch (TypeError $e) {
  180. throw new BadResponseData($e->getMessage(), $e->getCode(), $e);
  181. }
  182. }
  183. }
  184.  
  185. namespace JmsfwkCommunicatorResources;
  186.  
  187. class SubscriptionInfo extends Resource
  188. {
  189. public $MailingListId;
  190. public $IsSubscribed;
  191. public $DateLastAction;
  192. public $SubscriptionSourceType = 'TestCase';
  193.  
  194. public function __construct($MailingListId, $IsSubscribed = true)
  195. {
  196. $this->MailingListId = $MailingListId;
  197. $this->IsSubscribed = $IsSubscribed;
  198. $this->DateLastAction = date("Y-m-dTH:i:s");
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement