@@ -6,7 +6,8 @@ discard block |
||
| 6 | 6 | use P2A\YourMembership\Core\Request; |
| 7 | 7 | use P2A\YourMembership\Core\Response; |
| 8 | 8 | |
| 9 | -class YourMembershipClient { |
|
| 9 | +class YourMembershipClient |
|
| 10 | +{ |
|
| 10 | 11 | |
| 11 | 12 | /** |
| 12 | 13 | * Guzzle Client |
@@ -36,7 +37,8 @@ discard block |
||
| 36 | 37 | * @param array $arguments Your Membership API Call Arguments |
| 37 | 38 | * @return Response |
| 38 | 39 | */ |
| 39 | - public function makeCall(string $method, array $arguments = []) { |
|
| 40 | + public function makeCall(string $method, array $arguments = []) |
|
| 41 | + { |
|
| 40 | 42 | |
| 41 | 43 | Request::$callId++; //Update the Call ID as they need to be unique per call |
| 42 | 44 | $request = $this->request->buildRequest($method, $arguments); |
@@ -8,56 +8,56 @@ |
||
| 8 | 8 | |
| 9 | 9 | class YourMembershipClient { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Guzzle Client |
|
| 13 | - * @var \GuzzleHttp\Client |
|
| 14 | - */ |
|
| 15 | - private $client; |
|
| 16 | - /** |
|
| 17 | - * YourMembership Request |
|
| 18 | - * @var Request |
|
| 19 | - */ |
|
| 20 | - private $request; |
|
| 21 | - |
|
| 22 | - public function __construct(Client $client, string $apiKey, string $saPasscode) |
|
| 23 | - { |
|
| 24 | - $this->client = $client; |
|
| 25 | - $this->request = new Request($apiKey, $saPasscode); |
|
| 26 | - $sessionID = $this->createSession(); |
|
| 27 | - Request::setSessionID($sessionID); |
|
| 28 | - |
|
| 29 | - } |
|
| 30 | - /** |
|
| 31 | - * Makes API Call to YourMembership |
|
| 32 | - * @method makeCall |
|
| 33 | - * @author PA |
|
| 34 | - * @date 2017-01-10 |
|
| 35 | - * @param string $method Your Membership API Method |
|
| 36 | - * @param array $arguments Your Membership API Call Arguments |
|
| 37 | - * @return Response |
|
| 38 | - */ |
|
| 39 | - public function makeCall(string $method, array $arguments = []) { |
|
| 40 | - |
|
| 41 | - Request::$callId++; //Update the Call ID as they need to be unique per call |
|
| 42 | - $request = $this->request->buildRequest($method, $arguments); |
|
| 43 | - |
|
| 44 | - $response = $this->client->send($request); |
|
| 45 | - |
|
| 46 | - return new Response($method, $response); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Creates a new Session with YourMembership |
|
| 51 | - * @method createSession |
|
| 52 | - * @author PA |
|
| 53 | - * @date 2017-01-10 |
|
| 54 | - * @return string SessionID |
|
| 55 | - */ |
|
| 56 | - private function createSession() : string |
|
| 57 | - { |
|
| 58 | - $response = $this->makeCall('Session.Create')->toObject(); |
|
| 59 | - return $response->SessionID; |
|
| 60 | - |
|
| 61 | - } |
|
| 11 | + /** |
|
| 12 | + * Guzzle Client |
|
| 13 | + * @var \GuzzleHttp\Client |
|
| 14 | + */ |
|
| 15 | + private $client; |
|
| 16 | + /** |
|
| 17 | + * YourMembership Request |
|
| 18 | + * @var Request |
|
| 19 | + */ |
|
| 20 | + private $request; |
|
| 21 | + |
|
| 22 | + public function __construct(Client $client, string $apiKey, string $saPasscode) |
|
| 23 | + { |
|
| 24 | + $this->client = $client; |
|
| 25 | + $this->request = new Request($apiKey, $saPasscode); |
|
| 26 | + $sessionID = $this->createSession(); |
|
| 27 | + Request::setSessionID($sessionID); |
|
| 28 | + |
|
| 29 | + } |
|
| 30 | + /** |
|
| 31 | + * Makes API Call to YourMembership |
|
| 32 | + * @method makeCall |
|
| 33 | + * @author PA |
|
| 34 | + * @date 2017-01-10 |
|
| 35 | + * @param string $method Your Membership API Method |
|
| 36 | + * @param array $arguments Your Membership API Call Arguments |
|
| 37 | + * @return Response |
|
| 38 | + */ |
|
| 39 | + public function makeCall(string $method, array $arguments = []) { |
|
| 40 | + |
|
| 41 | + Request::$callId++; //Update the Call ID as they need to be unique per call |
|
| 42 | + $request = $this->request->buildRequest($method, $arguments); |
|
| 43 | + |
|
| 44 | + $response = $this->client->send($request); |
|
| 45 | + |
|
| 46 | + return new Response($method, $response); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Creates a new Session with YourMembership |
|
| 51 | + * @method createSession |
|
| 52 | + * @author PA |
|
| 53 | + * @date 2017-01-10 |
|
| 54 | + * @return string SessionID |
|
| 55 | + */ |
|
| 56 | + private function createSession() : string |
|
| 57 | + { |
|
| 58 | + $response = $this->makeCall('Session.Create')->toObject(); |
|
| 59 | + return $response->SessionID; |
|
| 60 | + |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | 63 | } |
@@ -55,7 +55,7 @@ |
||
| 55 | 55 | */ |
| 56 | 56 | private function createSession() : string |
| 57 | 57 | { |
| 58 | - $response = $this->makeCall('Session.Create')->toObject(); |
|
| 58 | + $response = $this->makeCall('Session.Create')->toObject(); |
|
| 59 | 59 | return $response->SessionID; |
| 60 | 60 | |
| 61 | 61 | } |
@@ -72,11 +72,11 @@ discard block |
||
| 72 | 72 | { |
| 73 | 73 | //Create Call Node |
| 74 | 74 | $call = new \SimpleXMLElement('<Call> </Call>'); |
| 75 | - $call->addAttribute('Method',$method); |
|
| 75 | + $call->addAttribute('Method', $method); |
|
| 76 | 76 | |
| 77 | 77 | //Add Arguments to the Call Node |
| 78 | 78 | foreach ($arguments as $key => $value) { |
| 79 | - $call->addChild($key,$value); |
|
| 79 | + $call->addChild($key, $value); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | return $call; |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $callPayload = $this->createCallPayload($method, $arguments); // Specific API Call Envelope |
| 94 | 94 | |
| 95 | 95 | // Put Api call into common envelope |
| 96 | - $this->sxmlAppend($xml,$callPayload); |
|
| 96 | + $this->sxmlAppend($xml, $callPayload); |
|
| 97 | 97 | |
| 98 | 98 | return $xml; |
| 99 | 99 | } |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | * @param SimpleXMLElement $from |
| 140 | 140 | * @return void |
| 141 | 141 | */ |
| 142 | - private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
|
| 142 | + private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
|
| 143 | 143 | $toDom = dom_import_simplexml($to); |
| 144 | 144 | $fromDom = dom_import_simplexml($from); |
| 145 | 145 | $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * @method buildBasePayload |
| 39 | 39 | * @author PA |
| 40 | 40 | * @date 2017-01-09 |
| 41 | - * @return SimpleXMLElement XML Envelope with necessary credential parameters |
|
| 41 | + * @return \SimpleXMLElement XML Envelope with necessary credential parameters |
|
| 42 | 42 | */ |
| 43 | 43 | public function buildBasePayload() : \SimpleXMLElement |
| 44 | 44 | { |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | * @date 2017-01-10 |
| 89 | 89 | * @param string $method Your Membership API Function Name |
| 90 | 90 | * @param array $arguments Your Membership Arguments |
| 91 | - * @return SimpleXMLElement |
|
| 91 | + * @return \SimpleXMLElement |
|
| 92 | 92 | */ |
| 93 | 93 | public function buildXMLBody(string $method, array $arguments) : \SimpleXMLElement |
| 94 | 94 | { |
@@ -143,8 +143,8 @@ discard block |
||
| 143 | 143 | * @method sxmlAppend |
| 144 | 144 | * @author PA |
| 145 | 145 | * @date 2017-01-09 |
| 146 | - * @param SimpleXMLElement $to |
|
| 147 | - * @param SimpleXMLElement $from |
|
| 146 | + * @param \SimpleXMLElement $to |
|
| 147 | + * @param \SimpleXMLElement $from |
|
| 148 | 148 | * @return void |
| 149 | 149 | */ |
| 150 | 150 | private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
@@ -8,51 +8,51 @@ 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 | - /** |
|
| 19 | - * Session ID use for YourMembership API |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - private static $sessionId = null; |
|
| 23 | - /** |
|
| 24 | - * Call Counter for Your Membership for a given session |
|
| 25 | - * @var integer |
|
| 26 | - */ |
|
| 27 | - public static $callId = 0; |
|
| 28 | - /** |
|
| 29 | - * API Key Used for YourMembership API |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - private $apiKey; |
|
| 33 | - /** |
|
| 34 | - * Sa Passcode is a supplementary API key used for YourMembership API |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $saPasscode; |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - public function __construct(string $apiKey, string $saPasscode) |
|
| 41 | - { |
|
| 42 | - $this->apiKey = $apiKey; |
|
| 43 | - $this->saPasscode = $saPasscode; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Create the Base Envelope for an API call to YourMembership |
|
| 48 | - * @method buildBasePayload |
|
| 49 | - * @author PA |
|
| 50 | - * @date 2017-01-09 |
|
| 51 | - * @return SimpleXMLElement XML Envelope with necessary credential parameters |
|
| 52 | - */ |
|
| 53 | - public function buildBasePayload() : \SimpleXMLElement |
|
| 54 | - { |
|
| 55 | - /* |
|
| 11 | + /** |
|
| 12 | + * Base URL |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + const BASE_URL = 'https://api.yourmembership.com'; |
|
| 16 | + const API_VERSION = '2.25'; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Session ID use for YourMembership API |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + private static $sessionId = null; |
|
| 23 | + /** |
|
| 24 | + * Call Counter for Your Membership for a given session |
|
| 25 | + * @var integer |
|
| 26 | + */ |
|
| 27 | + public static $callId = 0; |
|
| 28 | + /** |
|
| 29 | + * API Key Used for YourMembership API |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + private $apiKey; |
|
| 33 | + /** |
|
| 34 | + * Sa Passcode is a supplementary API key used for YourMembership API |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $saPasscode; |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + public function __construct(string $apiKey, string $saPasscode) |
|
| 41 | + { |
|
| 42 | + $this->apiKey = $apiKey; |
|
| 43 | + $this->saPasscode = $saPasscode; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Create the Base Envelope for an API call to YourMembership |
|
| 48 | + * @method buildBasePayload |
|
| 49 | + * @author PA |
|
| 50 | + * @date 2017-01-09 |
|
| 51 | + * @return SimpleXMLElement XML Envelope with necessary credential parameters |
|
| 52 | + */ |
|
| 53 | + public function buildBasePayload() : \SimpleXMLElement |
|
| 54 | + { |
|
| 55 | + /* |
|
| 56 | 56 | <YourMembership> |
| 57 | 57 | <Version>2.25</Version> |
| 58 | 58 | <ApiKey>3D638C5F-CCE2-4638-A2C1-355FA7BBC917</ApiKey> |
@@ -60,141 +60,141 @@ discard block |
||
| 60 | 60 | <SaPasscode>************</SaPasscode> |
| 61 | 61 | </YourMembership> |
| 62 | 62 | */ |
| 63 | - $xml = new \SimpleXMLElement('<YourMembership></YourMembership>'); |
|
| 64 | - $xml->addChild('Version', self::API_VERSION); |
|
| 65 | - $xml->addChild('ApiKey', $this->apiKey); |
|
| 66 | - $xml->addChild('CallID', self::$callId); |
|
| 67 | - $xml->addChild('SaPasscode', $this->saPasscode); |
|
| 68 | - |
|
| 69 | - return $xml; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Generates the XML for a API method call within |
|
| 74 | - * @method createCallPayload |
|
| 75 | - * @author PA |
|
| 76 | - * @date 2017-01-09 |
|
| 77 | - * @param string $method YourMembership API Function Name |
|
| 78 | - * @param array $arguments Array of Arguments to be passed as part of the YourMembership "Call" |
|
| 79 | - * @return \SimpleXMLElement |
|
| 80 | - */ |
|
| 81 | - public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement |
|
| 82 | - { |
|
| 83 | - //Create Call Node |
|
| 84 | - $call = new \SimpleXMLElement('<Call> </Call>'); |
|
| 85 | - $call->addAttribute('Method',$method); |
|
| 86 | - |
|
| 87 | - //Add Arguments to the Call Node |
|
| 88 | - foreach ($arguments as $key => $value) { |
|
| 89 | - $call->addChild($key,$value); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - return $call; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Builds The XML Request Body for the Your Membership API Call |
|
| 97 | - * @method buildXMLBody |
|
| 98 | - * @author PA |
|
| 99 | - * @date 2017-01-10 |
|
| 100 | - * @param string $method Your Membership API Function Name |
|
| 101 | - * @param array $arguments Your Membership Arguments |
|
| 102 | - * @return SimpleXMLElement |
|
| 103 | - */ |
|
| 104 | - public function buildXMLBody(string $method, array $arguments) : \SimpleXMLElement |
|
| 105 | - { |
|
| 106 | - $xml = $this->buildBasePayload(); // Common Envelope |
|
| 107 | - |
|
| 108 | - if ($this->isSessionRequiredForMethod($method)) { |
|
| 109 | - $xml = $this->addSessionIdToRequest($xml); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $callPayload = $this->createCallPayload($method, $arguments); // Specific API Call Envelope |
|
| 113 | - |
|
| 114 | - // Put Api call into common envelope |
|
| 115 | - $this->sxmlAppend($xml,$callPayload); |
|
| 116 | - |
|
| 117 | - return $xml; |
|
| 118 | - } |
|
| 119 | - /** |
|
| 120 | - * Builds a Guzzle Request Object |
|
| 121 | - * @method buildRequest |
|
| 122 | - * @author PA |
|
| 123 | - * @date 2017-01-11 |
|
| 124 | - * @param string $method YourMembership API Method |
|
| 125 | - * @param array $arguments YourMembership API Method Call Arguments |
|
| 126 | - * @return \GuzzleHttp\Psr7\Request Guzzle Request Object |
|
| 127 | - */ |
|
| 128 | - public function buildRequest(string $method, array $arguments) : \GuzzleHttp\Psr7\Request |
|
| 129 | - { |
|
| 130 | - $requestBody = $this->buildXMLBody($method, $arguments)->asXML(); |
|
| 131 | - return new GuzzleRequest('POST', self::BASE_URL, ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF8'], $requestBody); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Checks if Request Requires Session ID |
|
| 136 | - * @method isSessionRequiredForMethod |
|
| 137 | - * @author PA |
|
| 138 | - * @date 2017-01-10 |
|
| 139 | - * @param string $method YourMembership API Method |
|
| 140 | - * @return bool |
|
| 141 | - */ |
|
| 142 | - public function isSessionRequiredForMethod(string $method) : bool |
|
| 143 | - { |
|
| 144 | - //TODO Add config Logic for what API Methods require Session ID |
|
| 145 | - return ($method != 'Session.Create'); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Helper for Deep Copy for of $from element into $to element for SimpleXML |
|
| 150 | - * @method sxmlAppend |
|
| 151 | - * @author PA |
|
| 152 | - * @date 2017-01-09 |
|
| 153 | - * @param SimpleXMLElement $to |
|
| 154 | - * @param SimpleXMLElement $from |
|
| 155 | - * @return void |
|
| 156 | - */ |
|
| 157 | - private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
|
| 158 | - $toDom = dom_import_simplexml($to); |
|
| 159 | - $fromDom = dom_import_simplexml($from); |
|
| 160 | - $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Adds the Session Variable to the given XML Request Payload |
|
| 165 | - * @method addSessionIdToRequest |
|
| 166 | - * @author PA |
|
| 167 | - * @date 2017-01-10 |
|
| 168 | - * @param \SimpleXMLElement $requestXML Base Request XML Payload |
|
| 169 | - */ |
|
| 170 | - private function addSessionIdToRequest(\SimpleXMLElement $requestXML) : \SimpleXMLElement |
|
| 171 | - { |
|
| 172 | - $requestXML->addChild('SessionID', self::$sessionId); |
|
| 173 | - return $requestXML; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Setter Method for SessionID |
|
| 179 | - * @method setSessionId |
|
| 180 | - * @author PA |
|
| 181 | - * @date 2017-01-10 |
|
| 182 | - * @param string $sessionId YourMembership Session ID |
|
| 183 | - */ |
|
| 184 | - public static function setSessionId(string $sessionId) |
|
| 185 | - { |
|
| 186 | - self::$sessionId = $sessionId; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Checks if we have an active session available |
|
| 191 | - * @method hasSession |
|
| 192 | - * @author PA |
|
| 193 | - * @date 2017-01-11 |
|
| 194 | - * @return boolean |
|
| 195 | - */ |
|
| 196 | - public function hasSession() { |
|
| 197 | - return !is_null(self::$sessionId); |
|
| 198 | - } |
|
| 63 | + $xml = new \SimpleXMLElement('<YourMembership></YourMembership>'); |
|
| 64 | + $xml->addChild('Version', self::API_VERSION); |
|
| 65 | + $xml->addChild('ApiKey', $this->apiKey); |
|
| 66 | + $xml->addChild('CallID', self::$callId); |
|
| 67 | + $xml->addChild('SaPasscode', $this->saPasscode); |
|
| 68 | + |
|
| 69 | + return $xml; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Generates the XML for a API method call within |
|
| 74 | + * @method createCallPayload |
|
| 75 | + * @author PA |
|
| 76 | + * @date 2017-01-09 |
|
| 77 | + * @param string $method YourMembership API Function Name |
|
| 78 | + * @param array $arguments Array of Arguments to be passed as part of the YourMembership "Call" |
|
| 79 | + * @return \SimpleXMLElement |
|
| 80 | + */ |
|
| 81 | + public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement |
|
| 82 | + { |
|
| 83 | + //Create Call Node |
|
| 84 | + $call = new \SimpleXMLElement('<Call> </Call>'); |
|
| 85 | + $call->addAttribute('Method',$method); |
|
| 86 | + |
|
| 87 | + //Add Arguments to the Call Node |
|
| 88 | + foreach ($arguments as $key => $value) { |
|
| 89 | + $call->addChild($key,$value); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + return $call; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Builds The XML Request Body for the Your Membership API Call |
|
| 97 | + * @method buildXMLBody |
|
| 98 | + * @author PA |
|
| 99 | + * @date 2017-01-10 |
|
| 100 | + * @param string $method Your Membership API Function Name |
|
| 101 | + * @param array $arguments Your Membership Arguments |
|
| 102 | + * @return SimpleXMLElement |
|
| 103 | + */ |
|
| 104 | + public function buildXMLBody(string $method, array $arguments) : \SimpleXMLElement |
|
| 105 | + { |
|
| 106 | + $xml = $this->buildBasePayload(); // Common Envelope |
|
| 107 | + |
|
| 108 | + if ($this->isSessionRequiredForMethod($method)) { |
|
| 109 | + $xml = $this->addSessionIdToRequest($xml); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $callPayload = $this->createCallPayload($method, $arguments); // Specific API Call Envelope |
|
| 113 | + |
|
| 114 | + // Put Api call into common envelope |
|
| 115 | + $this->sxmlAppend($xml,$callPayload); |
|
| 116 | + |
|
| 117 | + return $xml; |
|
| 118 | + } |
|
| 119 | + /** |
|
| 120 | + * Builds a Guzzle Request Object |
|
| 121 | + * @method buildRequest |
|
| 122 | + * @author PA |
|
| 123 | + * @date 2017-01-11 |
|
| 124 | + * @param string $method YourMembership API Method |
|
| 125 | + * @param array $arguments YourMembership API Method Call Arguments |
|
| 126 | + * @return \GuzzleHttp\Psr7\Request Guzzle Request Object |
|
| 127 | + */ |
|
| 128 | + public function buildRequest(string $method, array $arguments) : \GuzzleHttp\Psr7\Request |
|
| 129 | + { |
|
| 130 | + $requestBody = $this->buildXMLBody($method, $arguments)->asXML(); |
|
| 131 | + return new GuzzleRequest('POST', self::BASE_URL, ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF8'], $requestBody); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Checks if Request Requires Session ID |
|
| 136 | + * @method isSessionRequiredForMethod |
|
| 137 | + * @author PA |
|
| 138 | + * @date 2017-01-10 |
|
| 139 | + * @param string $method YourMembership API Method |
|
| 140 | + * @return bool |
|
| 141 | + */ |
|
| 142 | + public function isSessionRequiredForMethod(string $method) : bool |
|
| 143 | + { |
|
| 144 | + //TODO Add config Logic for what API Methods require Session ID |
|
| 145 | + return ($method != 'Session.Create'); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Helper for Deep Copy for of $from element into $to element for SimpleXML |
|
| 150 | + * @method sxmlAppend |
|
| 151 | + * @author PA |
|
| 152 | + * @date 2017-01-09 |
|
| 153 | + * @param SimpleXMLElement $to |
|
| 154 | + * @param SimpleXMLElement $from |
|
| 155 | + * @return void |
|
| 156 | + */ |
|
| 157 | + private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
|
| 158 | + $toDom = dom_import_simplexml($to); |
|
| 159 | + $fromDom = dom_import_simplexml($from); |
|
| 160 | + $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Adds the Session Variable to the given XML Request Payload |
|
| 165 | + * @method addSessionIdToRequest |
|
| 166 | + * @author PA |
|
| 167 | + * @date 2017-01-10 |
|
| 168 | + * @param \SimpleXMLElement $requestXML Base Request XML Payload |
|
| 169 | + */ |
|
| 170 | + private function addSessionIdToRequest(\SimpleXMLElement $requestXML) : \SimpleXMLElement |
|
| 171 | + { |
|
| 172 | + $requestXML->addChild('SessionID', self::$sessionId); |
|
| 173 | + return $requestXML; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Setter Method for SessionID |
|
| 179 | + * @method setSessionId |
|
| 180 | + * @author PA |
|
| 181 | + * @date 2017-01-10 |
|
| 182 | + * @param string $sessionId YourMembership Session ID |
|
| 183 | + */ |
|
| 184 | + public static function setSessionId(string $sessionId) |
|
| 185 | + { |
|
| 186 | + self::$sessionId = $sessionId; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Checks if we have an active session available |
|
| 191 | + * @method hasSession |
|
| 192 | + * @author PA |
|
| 193 | + * @date 2017-01-11 |
|
| 194 | + * @return boolean |
|
| 195 | + */ |
|
| 196 | + public function hasSession() { |
|
| 197 | + return !is_null(self::$sessionId); |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | 200 | }; |
@@ -154,7 +154,8 @@ discard block |
||
| 154 | 154 | * @param SimpleXMLElement $from |
| 155 | 155 | * @return void |
| 156 | 156 | */ |
| 157 | - private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
|
| 157 | + private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) |
|
| 158 | + { |
|
| 158 | 159 | $toDom = dom_import_simplexml($to); |
| 159 | 160 | $fromDom = dom_import_simplexml($from); |
| 160 | 161 | $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
@@ -193,7 +194,8 @@ discard block |
||
| 193 | 194 | * @date 2017-01-11 |
| 194 | 195 | * @return boolean |
| 195 | 196 | */ |
| 196 | - public function hasSession() { |
|
| 197 | + public function hasSession() |
|
| 198 | + { |
|
| 197 | 199 | return !is_null(self::$sessionId); |
| 198 | 200 | } |
| 199 | 201 | |
@@ -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 | |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | private $method; |
| 11 | 11 | private $response; |
| 12 | 12 | |
| 13 | - public function __construct(string $method, $response) |
|
| 13 | + public function __construct(string $method, $response) |
|
| 14 | 14 | { |
| 15 | 15 | $this->method = $method; |
| 16 | 16 | |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function getError() : string |
| 54 | 54 | { |
| 55 | - return (string)$this->response->ErrDesc; |
|
| 55 | + return (string) $this->response->ErrDesc; |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | if (isset($this->response->{$this->method}->Results)) { |
| 116 | 116 | $attributes = $this->response->{$this->method}->Results->attributes(); |
| 117 | - $count = (int) $attributes['ResultTotal'] ?? false; |
|
| 117 | + $count = (int) $attributes['ResultTotal'] ?? false; |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | return $count; |
@@ -7,117 +7,117 @@ |
||
| 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 | - { |
|
| 15 | - $this->method = $method; |
|
| 13 | + public function __construct(string $method, $response) |
|
| 14 | + { |
|
| 15 | + $this->method = $method; |
|
| 16 | 16 | |
| 17 | - $body = $response->getBody()->getContents(); |
|
| 18 | - $this->response = new \SimpleXMLElement($body); |
|
| 17 | + $body = $response->getBody()->getContents(); |
|
| 18 | + $this->response = new \SimpleXMLElement($body); |
|
| 19 | 19 | |
| 20 | - } |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Checks if the response contains an Error |
|
| 24 | - * @method hasError |
|
| 25 | - * @author PA |
|
| 26 | - * @date 2017-01-10 |
|
| 27 | - * @return bool hasError |
|
| 28 | - */ |
|
| 29 | - public function hasError() : bool |
|
| 30 | - { |
|
| 31 | - return ($this->getErrorCode() != 0); |
|
| 32 | - } |
|
| 22 | + /** |
|
| 23 | + * Checks if the response contains an Error |
|
| 24 | + * @method hasError |
|
| 25 | + * @author PA |
|
| 26 | + * @date 2017-01-10 |
|
| 27 | + * @return bool hasError |
|
| 28 | + */ |
|
| 29 | + public function hasError() : bool |
|
| 30 | + { |
|
| 31 | + return ($this->getErrorCode() != 0); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Fetches the Error Code from the Response |
|
| 36 | - * @method getErrorCode |
|
| 37 | - * @author PA |
|
| 38 | - * @date 2017-01-10 |
|
| 39 | - * @return int Error Code |
|
| 40 | - */ |
|
| 41 | - public function getErrorCode() : int |
|
| 42 | - { |
|
| 43 | - return (int) $this->response->ErrCode; |
|
| 44 | - } |
|
| 34 | + /** |
|
| 35 | + * Fetches the Error Code from the Response |
|
| 36 | + * @method getErrorCode |
|
| 37 | + * @author PA |
|
| 38 | + * @date 2017-01-10 |
|
| 39 | + * @return int Error Code |
|
| 40 | + */ |
|
| 41 | + public function getErrorCode() : int |
|
| 42 | + { |
|
| 43 | + return (int) $this->response->ErrCode; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Fetches the Error Message From Response |
|
| 48 | - * @method getError |
|
| 49 | - * @author PA |
|
| 50 | - * @date 2017-01-10 |
|
| 51 | - * @return string Error Message |
|
| 52 | - */ |
|
| 53 | - public function getError() : string |
|
| 54 | - { |
|
| 55 | - return (string)$this->response->ErrDesc; |
|
| 56 | - } |
|
| 46 | + /** |
|
| 47 | + * Fetches the Error Message From Response |
|
| 48 | + * @method getError |
|
| 49 | + * @author PA |
|
| 50 | + * @date 2017-01-10 |
|
| 51 | + * @return string Error Message |
|
| 52 | + */ |
|
| 53 | + public function getError() : string |
|
| 54 | + { |
|
| 55 | + return (string)$this->response->ErrDesc; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Converts the response to an Array |
|
| 60 | - * @method toArray |
|
| 61 | - * @throws YourMembershipException |
|
| 62 | - * @author PA |
|
| 63 | - * @date 2017-01-10 |
|
| 64 | - * @return array Response |
|
| 65 | - */ |
|
| 66 | - public function toArray() : array |
|
| 67 | - { |
|
| 68 | - return $this->unwrapXMLObject(true); |
|
| 69 | - } |
|
| 58 | + /** |
|
| 59 | + * Converts the response to an Array |
|
| 60 | + * @method toArray |
|
| 61 | + * @throws YourMembershipException |
|
| 62 | + * @author PA |
|
| 63 | + * @date 2017-01-10 |
|
| 64 | + * @return array Response |
|
| 65 | + */ |
|
| 66 | + public function toArray() : array |
|
| 67 | + { |
|
| 68 | + return $this->unwrapXMLObject(true); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Converts the response to an Object |
|
| 73 | - * @method toObject |
|
| 74 | - * @throws YourMembershipException |
|
| 75 | - * @author PA |
|
| 76 | - * @date 2017-01-11 |
|
| 77 | - * @return stdClass Response |
|
| 78 | - */ |
|
| 79 | - public function toObject() : \stdClass |
|
| 80 | - { |
|
| 81 | - return $this->unwrapXMLObject(false); |
|
| 82 | - } |
|
| 71 | + /** |
|
| 72 | + * Converts the response to an Object |
|
| 73 | + * @method toObject |
|
| 74 | + * @throws YourMembershipException |
|
| 75 | + * @author PA |
|
| 76 | + * @date 2017-01-11 |
|
| 77 | + * @return stdClass Response |
|
| 78 | + */ |
|
| 79 | + public function toObject() : \stdClass |
|
| 80 | + { |
|
| 81 | + return $this->unwrapXMLObject(false); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Unwraps XML Object into either StdClass or Array |
|
| 86 | - * Lossy conversion, attributes are lost from XML |
|
| 87 | - * |
|
| 88 | - * @method unwrapXMLObject |
|
| 89 | - * @throws YourMembershipException |
|
| 90 | - * @author PA |
|
| 91 | - * @date 2017-01-11 |
|
| 92 | - * @param bool $asArray unwrap the object into an array instead of object |
|
| 93 | - * @return mixed|null Unwrapped Response |
|
| 94 | - */ |
|
| 95 | - private function unwrapXMLObject(bool $asArray) |
|
| 96 | - { |
|
| 97 | - //We cannot unwrap objects that have errors, so throw an exception |
|
| 98 | - if ($this->hasError()) { |
|
| 99 | - throw new YourMembershipException($this->getError(), $this->getErrorCode(), $this->method); |
|
| 100 | - } |
|
| 84 | + /** |
|
| 85 | + * Unwraps XML Object into either StdClass or Array |
|
| 86 | + * Lossy conversion, attributes are lost from XML |
|
| 87 | + * |
|
| 88 | + * @method unwrapXMLObject |
|
| 89 | + * @throws YourMembershipException |
|
| 90 | + * @author PA |
|
| 91 | + * @date 2017-01-11 |
|
| 92 | + * @param bool $asArray unwrap the object into an array instead of object |
|
| 93 | + * @return mixed|null Unwrapped Response |
|
| 94 | + */ |
|
| 95 | + private function unwrapXMLObject(bool $asArray) |
|
| 96 | + { |
|
| 97 | + //We cannot unwrap objects that have errors, so throw an exception |
|
| 98 | + if ($this->hasError()) { |
|
| 99 | + throw new YourMembershipException($this->getError(), $this->getErrorCode(), $this->method); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - return json_decode(json_encode($this->response->{$this->method}), $asArray); |
|
| 103 | - } |
|
| 104 | - /** |
|
| 105 | - * Returns the Result Count |
|
| 106 | - * @method getResultCount |
|
| 107 | - * @author PA |
|
| 108 | - * @date 2017-01-10 |
|
| 109 | - * @return int|false false if no ResultCount is present |
|
| 110 | - */ |
|
| 111 | - public function getResultCount() : int |
|
| 112 | - { |
|
| 113 | - $count = false; |
|
| 102 | + return json_decode(json_encode($this->response->{$this->method}), $asArray); |
|
| 103 | + } |
|
| 104 | + /** |
|
| 105 | + * Returns the Result Count |
|
| 106 | + * @method getResultCount |
|
| 107 | + * @author PA |
|
| 108 | + * @date 2017-01-10 |
|
| 109 | + * @return int|false false if no ResultCount is present |
|
| 110 | + */ |
|
| 111 | + public function getResultCount() : int |
|
| 112 | + { |
|
| 113 | + $count = false; |
|
| 114 | 114 | |
| 115 | - if (isset($this->response->{$this->method}->Results)) { |
|
| 116 | - $attributes = $this->response->{$this->method}->Results->attributes(); |
|
| 117 | - $count = (int) $attributes['ResultTotal'] ?? false; |
|
| 118 | - } |
|
| 115 | + if (isset($this->response->{$this->method}->Results)) { |
|
| 116 | + $attributes = $this->response->{$this->method}->Results->attributes(); |
|
| 117 | + $count = (int) $attributes['ResultTotal'] ?? false; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - return $count; |
|
| 121 | - } |
|
| 120 | + return $count; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | 123 | } |
@@ -22,10 +22,10 @@ |
||
| 22 | 22 | // |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - public function boot() |
|
| 26 | - { |
|
| 27 | - $this->package('phone2action/ym-api'); |
|
| 28 | - } |
|
| 25 | + public function boot() |
|
| 26 | + { |
|
| 27 | + $this->package('phone2action/ym-api'); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * Get the services provided by the provider. |
@@ -3,7 +3,8 @@ |
||
| 3 | 3 | |
| 4 | 4 | use Illuminate\Support\ServiceProvider; |
| 5 | 5 | |
| 6 | -class YourMembershipServiceProvider extends ServiceProvider { |
|
| 6 | +class YourMembershipServiceProvider extends ServiceProvider |
|
| 7 | +{ |
|
| 7 | 8 | |
| 8 | 9 | /** |
| 9 | 10 | * Indicates if loading of the provider is deferred. |