@@ -37,7 +37,8 @@ |
||
| 37 | 37 | * @param array $arguments Your Membership API Call Arguments |
| 38 | 38 | * @return Response |
| 39 | 39 | */ |
| 40 | - public function makeCall(string $method, array $arguments = []) { |
|
| 40 | + public function makeCall(string $method, array $arguments = []) |
|
| 41 | + { |
|
| 41 | 42 | |
| 42 | 43 | Request::$callId++; //Update the Call ID as they need to be unique per call |
| 43 | 44 | $request = $this->request->buildRequest($method, $arguments); |
@@ -8,41 +8,41 @@ discard block |
||
| 8 | 8 | class Request |
| 9 | 9 | { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Base URL |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - const BASE_URL = 'https://api.yourmembership.com'; |
|
| 16 | - const API_VERSION = '2.25'; |
|
| 17 | - |
|
| 18 | - private static $sessionId; |
|
| 19 | - /** |
|
| 20 | - * Call Counter for Your Membership for a given session |
|
| 21 | - * @var integer |
|
| 22 | - */ |
|
| 23 | - public static $callId = 0; |
|
| 24 | - |
|
| 25 | - private $apiKey; |
|
| 26 | - private $saPasscode; |
|
| 27 | - private $version; |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - function __construct(string $apiKey, string $saPasscode) |
|
| 31 | - { |
|
| 32 | - $this->apiKey = $apiKey; |
|
| 33 | - $this->saPasscode = $saPasscode; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Create the Base Envelope for an API call to YourMembership |
|
| 38 | - * @method buildBasePayload |
|
| 39 | - * @author PA |
|
| 40 | - * @date 2017-01-09 |
|
| 41 | - * @return \SimpleXMLElement XML Envelope with necessary credential parameters |
|
| 42 | - */ |
|
| 43 | - public function buildBasePayload() : \SimpleXMLElement |
|
| 44 | - { |
|
| 45 | - /* |
|
| 11 | + /** |
|
| 12 | + * Base URL |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + const BASE_URL = 'https://api.yourmembership.com'; |
|
| 16 | + const API_VERSION = '2.25'; |
|
| 17 | + |
|
| 18 | + private static $sessionId; |
|
| 19 | + /** |
|
| 20 | + * Call Counter for Your Membership for a given session |
|
| 21 | + * @var integer |
|
| 22 | + */ |
|
| 23 | + public static $callId = 0; |
|
| 24 | + |
|
| 25 | + private $apiKey; |
|
| 26 | + private $saPasscode; |
|
| 27 | + private $version; |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + function __construct(string $apiKey, string $saPasscode) |
|
| 31 | + { |
|
| 32 | + $this->apiKey = $apiKey; |
|
| 33 | + $this->saPasscode = $saPasscode; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Create the Base Envelope for an API call to YourMembership |
|
| 38 | + * @method buildBasePayload |
|
| 39 | + * @author PA |
|
| 40 | + * @date 2017-01-09 |
|
| 41 | + * @return \SimpleXMLElement XML Envelope with necessary credential parameters |
|
| 42 | + */ |
|
| 43 | + public function buildBasePayload() : \SimpleXMLElement |
|
| 44 | + { |
|
| 45 | + /* |
|
| 46 | 46 | <YourMembership> |
| 47 | 47 | <Version>2.25</Version> |
| 48 | 48 | <ApiKey>3D638C5F-CCE2-4638-A2C1-355FA7BBC917</ApiKey> |
@@ -50,125 +50,125 @@ discard block |
||
| 50 | 50 | <SaPasscode>************</SaPasscode> |
| 51 | 51 | </YourMembership> |
| 52 | 52 | */ |
| 53 | - $xml = new \SimpleXMLElement('<YourMembership></YourMembership>'); |
|
| 54 | - $xml->addChild('Version', self::API_VERSION); |
|
| 55 | - $xml->addChild('ApiKey', $this->apiKey); |
|
| 56 | - $xml->addChild('CallID', self::$callId); |
|
| 57 | - $xml->addChild('SaPasscode', $this->saPasscode); |
|
| 58 | - |
|
| 59 | - return $xml; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Generates the XML for a API method call within |
|
| 64 | - * @method createCallPayload |
|
| 65 | - * @author PA |
|
| 66 | - * @date 2017-01-09 |
|
| 67 | - * @param string $method YourMembership API Function Name |
|
| 68 | - * @param array $arguments Array of Arguments to be passed as part of the YourMembership "Call" |
|
| 69 | - * @return \SimpleXMLElement |
|
| 70 | - */ |
|
| 71 | - public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement |
|
| 72 | - { |
|
| 73 | - //Create Call Node |
|
| 74 | - $call = new \SimpleXMLElement('<Call> </Call>'); |
|
| 75 | - $call->addAttribute('Method', $method); |
|
| 76 | - |
|
| 77 | - //Add Arguments to the Call Node |
|
| 78 | - foreach ($arguments as $key => $value) { |
|
| 79 | - $call->addChild($key, $value); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return $call; |
|
| 83 | - } |
|
| 84 | - /** |
|
| 85 | - * Builds The XML Request Body for the Your Membership API Call |
|
| 86 | - * @method buildXMLBody |
|
| 87 | - * @author PA |
|
| 88 | - * @date 2017-01-10 |
|
| 89 | - * @param string $method Your Membership API Function Name |
|
| 90 | - * @param array $arguments Your Membership Arguments |
|
| 91 | - * @return \SimpleXMLElement |
|
| 92 | - */ |
|
| 93 | - public function buildXMLBody(string $method, array $arguments) : \SimpleXMLElement |
|
| 94 | - { |
|
| 95 | - $xml = $this->buildBasePayload(); // Common Envelope |
|
| 96 | - |
|
| 97 | - if ($this->isSessionRequiredForMethod($method)) { |
|
| 98 | - $xml = $this->addSessionIdToRequest($xml); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $callPayload = $this->createCallPayload($method, $arguments); // Specific API Call Envelope |
|
| 102 | - |
|
| 103 | - // Put Api call into common envelope |
|
| 104 | - $this->sxmlAppend($xml, $callPayload); |
|
| 105 | - |
|
| 106 | - return $xml; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - public function buildRequest(string $method, array $arguments) : GuzzleRequest |
|
| 110 | - { |
|
| 111 | - $requestBody = $this->buildXMLBody($method, $arguments)->asXML(); |
|
| 112 | - codecept_debug($requestBody); |
|
| 113 | - return new GuzzleRequest('POST', self::BASE_URL, ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF8'], $requestBody); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Checks if Request Requires Session ID |
|
| 118 | - * @method isSessionRequiredForMethod |
|
| 119 | - * @author PA |
|
| 120 | - * @date 2017-01-10 |
|
| 121 | - * @param string $method YourMembership API Method |
|
| 122 | - * @return bool |
|
| 123 | - */ |
|
| 124 | - public function isSessionRequiredForMethod(string $method) : bool |
|
| 125 | - { |
|
| 126 | - return ($method != 'Session.Create'); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Setter Method for SessionID |
|
| 131 | - * @method setSessionId |
|
| 132 | - * @author PA |
|
| 133 | - * @date 2017-01-10 |
|
| 134 | - * @param string $sessionId [description] |
|
| 135 | - */ |
|
| 136 | - public static function setSessionId(string $sessionId) |
|
| 137 | - { |
|
| 138 | - self::$sessionId = $sessionId; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Helper for Deep Copy for of $from element into $to element for SimpleXML |
|
| 143 | - * @method sxmlAppend |
|
| 144 | - * @author PA |
|
| 145 | - * @date 2017-01-09 |
|
| 146 | - * @param \SimpleXMLElement $to |
|
| 147 | - * @param \SimpleXMLElement $from |
|
| 148 | - * @return void |
|
| 149 | - */ |
|
| 150 | - private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
|
| 151 | - $toDom = dom_import_simplexml($to); |
|
| 152 | - $fromDom = dom_import_simplexml($from); |
|
| 153 | - $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Adds the Session Variable to the given XML Request Payload |
|
| 158 | - * @method addSessionIdToRequest |
|
| 159 | - * @author PA |
|
| 160 | - * @date 2017-01-10 |
|
| 161 | - * @param \SimpleXMLElement $requestXML Base Request XML Payload |
|
| 162 | - */ |
|
| 163 | - private function addSessionIdToRequest(\SimpleXMLElement $requestXML) : \SimpleXMLElement |
|
| 164 | - { |
|
| 165 | - $requestXML->addChild('SessionID', self::$sessionId); |
|
| 166 | - return $requestXML; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - public function isSessionActive() |
|
| 170 | - { |
|
| 171 | - return self::$sessionID; |
|
| 172 | - } |
|
| 53 | + $xml = new \SimpleXMLElement('<YourMembership></YourMembership>'); |
|
| 54 | + $xml->addChild('Version', self::API_VERSION); |
|
| 55 | + $xml->addChild('ApiKey', $this->apiKey); |
|
| 56 | + $xml->addChild('CallID', self::$callId); |
|
| 57 | + $xml->addChild('SaPasscode', $this->saPasscode); |
|
| 58 | + |
|
| 59 | + return $xml; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Generates the XML for a API method call within |
|
| 64 | + * @method createCallPayload |
|
| 65 | + * @author PA |
|
| 66 | + * @date 2017-01-09 |
|
| 67 | + * @param string $method YourMembership API Function Name |
|
| 68 | + * @param array $arguments Array of Arguments to be passed as part of the YourMembership "Call" |
|
| 69 | + * @return \SimpleXMLElement |
|
| 70 | + */ |
|
| 71 | + public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement |
|
| 72 | + { |
|
| 73 | + //Create Call Node |
|
| 74 | + $call = new \SimpleXMLElement('<Call> </Call>'); |
|
| 75 | + $call->addAttribute('Method', $method); |
|
| 76 | + |
|
| 77 | + //Add Arguments to the Call Node |
|
| 78 | + foreach ($arguments as $key => $value) { |
|
| 79 | + $call->addChild($key, $value); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return $call; |
|
| 83 | + } |
|
| 84 | + /** |
|
| 85 | + * Builds The XML Request Body for the Your Membership API Call |
|
| 86 | + * @method buildXMLBody |
|
| 87 | + * @author PA |
|
| 88 | + * @date 2017-01-10 |
|
| 89 | + * @param string $method Your Membership API Function Name |
|
| 90 | + * @param array $arguments Your Membership Arguments |
|
| 91 | + * @return \SimpleXMLElement |
|
| 92 | + */ |
|
| 93 | + public function buildXMLBody(string $method, array $arguments) : \SimpleXMLElement |
|
| 94 | + { |
|
| 95 | + $xml = $this->buildBasePayload(); // Common Envelope |
|
| 96 | + |
|
| 97 | + if ($this->isSessionRequiredForMethod($method)) { |
|
| 98 | + $xml = $this->addSessionIdToRequest($xml); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $callPayload = $this->createCallPayload($method, $arguments); // Specific API Call Envelope |
|
| 102 | + |
|
| 103 | + // Put Api call into common envelope |
|
| 104 | + $this->sxmlAppend($xml, $callPayload); |
|
| 105 | + |
|
| 106 | + return $xml; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + public function buildRequest(string $method, array $arguments) : GuzzleRequest |
|
| 110 | + { |
|
| 111 | + $requestBody = $this->buildXMLBody($method, $arguments)->asXML(); |
|
| 112 | + codecept_debug($requestBody); |
|
| 113 | + return new GuzzleRequest('POST', self::BASE_URL, ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF8'], $requestBody); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Checks if Request Requires Session ID |
|
| 118 | + * @method isSessionRequiredForMethod |
|
| 119 | + * @author PA |
|
| 120 | + * @date 2017-01-10 |
|
| 121 | + * @param string $method YourMembership API Method |
|
| 122 | + * @return bool |
|
| 123 | + */ |
|
| 124 | + public function isSessionRequiredForMethod(string $method) : bool |
|
| 125 | + { |
|
| 126 | + return ($method != 'Session.Create'); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Setter Method for SessionID |
|
| 131 | + * @method setSessionId |
|
| 132 | + * @author PA |
|
| 133 | + * @date 2017-01-10 |
|
| 134 | + * @param string $sessionId [description] |
|
| 135 | + */ |
|
| 136 | + public static function setSessionId(string $sessionId) |
|
| 137 | + { |
|
| 138 | + self::$sessionId = $sessionId; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Helper for Deep Copy for of $from element into $to element for SimpleXML |
|
| 143 | + * @method sxmlAppend |
|
| 144 | + * @author PA |
|
| 145 | + * @date 2017-01-09 |
|
| 146 | + * @param \SimpleXMLElement $to |
|
| 147 | + * @param \SimpleXMLElement $from |
|
| 148 | + * @return void |
|
| 149 | + */ |
|
| 150 | + private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
|
| 151 | + $toDom = dom_import_simplexml($to); |
|
| 152 | + $fromDom = dom_import_simplexml($from); |
|
| 153 | + $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Adds the Session Variable to the given XML Request Payload |
|
| 158 | + * @method addSessionIdToRequest |
|
| 159 | + * @author PA |
|
| 160 | + * @date 2017-01-10 |
|
| 161 | + * @param \SimpleXMLElement $requestXML Base Request XML Payload |
|
| 162 | + */ |
|
| 163 | + private function addSessionIdToRequest(\SimpleXMLElement $requestXML) : \SimpleXMLElement |
|
| 164 | + { |
|
| 165 | + $requestXML->addChild('SessionID', self::$sessionId); |
|
| 166 | + return $requestXML; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + public function isSessionActive() |
|
| 170 | + { |
|
| 171 | + return self::$sessionID; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | 174 | }; |
@@ -147,7 +147,8 @@ |
||
| 147 | 147 | * @param \SimpleXMLElement $from |
| 148 | 148 | * @return void |
| 149 | 149 | */ |
| 150 | - private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
|
| 150 | + private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) |
|
| 151 | + { |
|
| 151 | 152 | $toDom = dom_import_simplexml($to); |
| 152 | 153 | $fromDom = dom_import_simplexml($from); |
| 153 | 154 | $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
@@ -11,13 +11,14 @@ |
||
| 11 | 11 | { |
| 12 | 12 | private $apiMethod; |
| 13 | 13 | |
| 14 | - public function __construct(string $message, int $code = 0, string $apiMethod) |
|
| 14 | + public function __construct(string $message, int $code = 0, string $apiMethod) |
|
| 15 | 15 | { |
| 16 | 16 | $this->apiMethod = $apiMethod; |
| 17 | 17 | parent::__construct($message, $code, null); |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - public function __toString() { |
|
| 20 | + public function __toString() |
|
| 21 | + { |
|
| 21 | 22 | return __CLASS__ . ": [{$this->apiMethod}]: {$this->message}\n"; |
| 22 | 23 | } |
| 23 | 24 | |
@@ -7,84 +7,84 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Response |
| 9 | 9 | { |
| 10 | - private $method; |
|
| 11 | - private $response; |
|
| 10 | + private $method; |
|
| 11 | + private $response; |
|
| 12 | 12 | |
| 13 | - public function __construct(string $method, $response) |
|
| 14 | - { |
|
| 13 | + public function __construct(string $method, $response) |
|
| 14 | + { |
|
| 15 | 15 | |
| 16 | - $this->method = $method; |
|
| 16 | + $this->method = $method; |
|
| 17 | 17 | |
| 18 | - $body = $response->getBody(); |
|
| 19 | - codecept_debug($body); |
|
| 20 | - $this->response = new \SimpleXMLElement($body); |
|
| 21 | - codecept_debug($this->response->asXml()); |
|
| 22 | - if ($this->hasError()) { |
|
| 23 | - throw new YourMembershipException($this->getError(), $this->getErrorCode(), $this->method); |
|
| 24 | - } |
|
| 25 | - } |
|
| 26 | - /** |
|
| 27 | - * Checks if the response contains an Error |
|
| 28 | - * @method hasError |
|
| 29 | - * @author PA |
|
| 30 | - * @date 2017-01-10 |
|
| 31 | - * @return bool hasError |
|
| 32 | - */ |
|
| 33 | - public function hasError() : bool |
|
| 34 | - { |
|
| 35 | - return ($this->getErrorCode() != 0); |
|
| 36 | - } |
|
| 18 | + $body = $response->getBody(); |
|
| 19 | + codecept_debug($body); |
|
| 20 | + $this->response = new \SimpleXMLElement($body); |
|
| 21 | + codecept_debug($this->response->asXml()); |
|
| 22 | + if ($this->hasError()) { |
|
| 23 | + throw new YourMembershipException($this->getError(), $this->getErrorCode(), $this->method); |
|
| 24 | + } |
|
| 25 | + } |
|
| 26 | + /** |
|
| 27 | + * Checks if the response contains an Error |
|
| 28 | + * @method hasError |
|
| 29 | + * @author PA |
|
| 30 | + * @date 2017-01-10 |
|
| 31 | + * @return bool hasError |
|
| 32 | + */ |
|
| 33 | + public function hasError() : bool |
|
| 34 | + { |
|
| 35 | + return ($this->getErrorCode() != 0); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Fetches the Error Code from the Response |
|
| 40 | - * @method getErrorCode |
|
| 41 | - * @author PA |
|
| 42 | - * @date 2017-01-10 |
|
| 43 | - * @return int Error Code |
|
| 44 | - */ |
|
| 45 | - public function getErrorCode() : int |
|
| 46 | - { |
|
| 47 | - return (int) $this->response->ErrCode; |
|
| 48 | - } |
|
| 49 | - /** |
|
| 50 | - * Fetches the Error Message From Response |
|
| 51 | - * @method getError |
|
| 52 | - * @author PA |
|
| 53 | - * @date 2017-01-10 |
|
| 54 | - * @return string Error Message |
|
| 55 | - */ |
|
| 56 | - public function getError() : string |
|
| 57 | - { |
|
| 58 | - return (string) $this->response->ErrDesc; |
|
| 59 | - } |
|
| 60 | - /** |
|
| 61 | - * Converts the response to an Array |
|
| 62 | - * @method toArray |
|
| 63 | - * @author PA |
|
| 64 | - * @date 2017-01-10 |
|
| 65 | - * @return array Response |
|
| 66 | - */ |
|
| 67 | - public function toArray() : array |
|
| 68 | - { |
|
| 69 | - return json_decode(json_encode($this->response->{$this->method}), TRUE); |
|
| 70 | - } |
|
| 71 | - /** |
|
| 72 | - * Returns the Result Count |
|
| 73 | - * @method getResultCount |
|
| 74 | - * @author PA |
|
| 75 | - * @date 2017-01-10 |
|
| 76 | - * @return int|false false if no ResultCount is present |
|
| 77 | - */ |
|
| 78 | - public function getResultCount() |
|
| 79 | - { |
|
| 80 | - $count = false; |
|
| 38 | + /** |
|
| 39 | + * Fetches the Error Code from the Response |
|
| 40 | + * @method getErrorCode |
|
| 41 | + * @author PA |
|
| 42 | + * @date 2017-01-10 |
|
| 43 | + * @return int Error Code |
|
| 44 | + */ |
|
| 45 | + public function getErrorCode() : int |
|
| 46 | + { |
|
| 47 | + return (int) $this->response->ErrCode; |
|
| 48 | + } |
|
| 49 | + /** |
|
| 50 | + * Fetches the Error Message From Response |
|
| 51 | + * @method getError |
|
| 52 | + * @author PA |
|
| 53 | + * @date 2017-01-10 |
|
| 54 | + * @return string Error Message |
|
| 55 | + */ |
|
| 56 | + public function getError() : string |
|
| 57 | + { |
|
| 58 | + return (string) $this->response->ErrDesc; |
|
| 59 | + } |
|
| 60 | + /** |
|
| 61 | + * Converts the response to an Array |
|
| 62 | + * @method toArray |
|
| 63 | + * @author PA |
|
| 64 | + * @date 2017-01-10 |
|
| 65 | + * @return array Response |
|
| 66 | + */ |
|
| 67 | + public function toArray() : array |
|
| 68 | + { |
|
| 69 | + return json_decode(json_encode($this->response->{$this->method}), TRUE); |
|
| 70 | + } |
|
| 71 | + /** |
|
| 72 | + * Returns the Result Count |
|
| 73 | + * @method getResultCount |
|
| 74 | + * @author PA |
|
| 75 | + * @date 2017-01-10 |
|
| 76 | + * @return int|false false if no ResultCount is present |
|
| 77 | + */ |
|
| 78 | + public function getResultCount() |
|
| 79 | + { |
|
| 80 | + $count = false; |
|
| 81 | 81 | |
| 82 | - if (isset($this->response->Results)) { |
|
| 83 | - $attributes = $this->response->Results->getAttributes(); |
|
| 84 | - $count = $attributes['ResultTotal'] ?? false; |
|
| 85 | - } |
|
| 82 | + if (isset($this->response->Results)) { |
|
| 83 | + $attributes = $this->response->Results->getAttributes(); |
|
| 84 | + $count = $attributes['ResultTotal'] ?? false; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - return $count; |
|
| 88 | - } |
|
| 87 | + return $count; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | 90 | } |