@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * Package Config Path |
16 | 16 | * @var string |
17 | 17 | */ |
18 | - private $packageConfigPath = __DIR__.'/config/yourmembership.php'; |
|
18 | + private $packageConfigPath = __DIR__ . '/config/yourmembership.php'; |
|
19 | 19 | /** |
20 | 20 | * Indicates if loading of the provider is deferred. |
21 | 21 | * |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | $this->packageConfigPath, $this->packageName |
35 | 35 | ); |
36 | 36 | |
37 | - $this->app->bind(YourMembershipClient::class, function ($app, $parameters) { |
|
37 | + $this->app->bind(YourMembershipClient::class, function($app, $parameters) { |
|
38 | 38 | $guzzleClient = new \GuzzleHttp\Client($app['config']['yourmembership']['guzzle-client']); |
39 | 39 | return new YourMembershipClient($guzzleClient, $parameters[0], $parameters[1]); |
40 | 40 | }); |
@@ -60,169 +60,169 @@ |
||
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 | - * @throws YourMembershipRequestException |
|
77 | - * @date 2017-01-09 |
|
78 | - * @param string $method YourMembership API Function Name |
|
79 | - * @param array $arguments Array of Arguments to be passed as part of the YourMembership "Call" |
|
80 | - * @return \SimpleXMLElement |
|
81 | - */ |
|
82 | - public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement |
|
83 | - { |
|
84 | - //Create Call Node |
|
85 | - $call = new \SimpleXMLElement('<Call> </Call>'); |
|
86 | - $call->addAttribute('Method', $method); |
|
87 | - |
|
88 | - //Add Arguments to the Call Node |
|
89 | - try { |
|
90 | - $call = $this->sxmlAddChildrenRecursive($call, $arguments); |
|
91 | - } catch (\Exception $e) { |
|
92 | - throw new YourMembershipRequestException($e->getMessage(), $e->getCode(), $e); |
|
93 | - } |
|
94 | - return $call; |
|
95 | - } |
|
96 | - /** |
|
97 | - * Recursively builds the array into a XML Tree |
|
98 | - * //NOTE Child arrays must be associative |
|
99 | - * @method sxmlAddChildrenRecursive |
|
100 | - * @author PA |
|
101 | - * @throws \Exception XML Parsing Exception |
|
102 | - * @date 2017-01-12 |
|
103 | - * @param \SimpleXMLElement $root Root XML Node |
|
104 | - * @param array $arguments Array of Arguments to be added to XML Node |
|
105 | - * @return \SimpleXMLElement Resulting XML Tree |
|
106 | - */ |
|
107 | - private function sxmlAddChildrenRecursive(\SimpleXMLElement $root, array $arguments) : \SimpleXMLElement |
|
108 | - { |
|
109 | - foreach ($arguments as $key => $value) { |
|
110 | - if (is_array($value)) { |
|
111 | - $child = new \SimpleXMLElement(sprintf('<%s></%s>', $key, $key)); |
|
112 | - $this->sxmlAddChildrenRecursive($child, $value); |
|
113 | - $this->sxmlAppend($root, $child); |
|
114 | - } else { |
|
115 | - $root->addChild($key, $value); |
|
116 | - } |
|
117 | - |
|
118 | - } |
|
119 | - return $root; |
|
120 | - } |
|
121 | - /** |
|
122 | - * Builds The XML Request Body for the Your Membership API Call |
|
123 | - * @method buildXMLBody |
|
124 | - * @author PA |
|
125 | - * @date 2017-01-10 |
|
126 | - * @param string $method Your Membership API Function Name |
|
127 | - * @param array $arguments Your Membership Arguments |
|
128 | - * @return \SimpleXMLElement |
|
129 | - */ |
|
130 | - public function buildXMLBody(string $method, array $arguments) : \SimpleXMLElement |
|
131 | - { |
|
132 | - $xml = $this->buildBasePayload(); // Common Envelope |
|
133 | - |
|
134 | - if ($this->isSessionRequiredForMethod($method)) { |
|
135 | - $xml = $this->addSessionIdToRequest($xml); |
|
136 | - } |
|
137 | - |
|
138 | - $callPayload = $this->createCallPayload($method, $arguments); // Specific API Call Envelope |
|
139 | - |
|
140 | - // Put Api call into common envelope |
|
141 | - $this->sxmlAppend($xml, $callPayload); |
|
142 | - |
|
143 | - return $xml; |
|
144 | - } |
|
145 | - /** |
|
146 | - * Builds a Guzzle Request Object |
|
147 | - * @method buildRequest |
|
148 | - * @author PA |
|
149 | - * @date 2017-01-11 |
|
150 | - * @param string $method YourMembership API Method |
|
151 | - * @param array $arguments YourMembership API Method Call Arguments |
|
152 | - * @return \GuzzleHttp\Psr7\Request Guzzle Request Object |
|
153 | - */ |
|
154 | - public function buildRequest(string $method, array $arguments) : \GuzzleHttp\Psr7\Request |
|
155 | - { |
|
156 | - $requestBody = $this->buildXMLBody($method, $arguments)->asXML(); |
|
157 | - return new GuzzleRequest('POST', self::BASE_URL, ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF8'], $requestBody); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Checks if Request Requires Session ID |
|
162 | - * @method isSessionRequiredForMethod |
|
163 | - * @author PA |
|
164 | - * @date 2017-01-10 |
|
165 | - * @param string $method YourMembership API Method |
|
166 | - * @return bool |
|
167 | - */ |
|
168 | - public function isSessionRequiredForMethod(string $method) : bool |
|
169 | - { |
|
170 | - //TODO Add config Logic for what API Methods require Session ID |
|
171 | - return ($method != 'Session.Create'); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Helper for Deep Copy for of $from element into $to element for SimpleXML |
|
176 | - * @method sxmlAppend |
|
177 | - * @author PA |
|
178 | - * @date 2017-01-09 |
|
179 | - * @param \SimpleXMLElement $to |
|
180 | - * @param \SimpleXMLElement $from |
|
181 | - * @return void |
|
182 | - */ |
|
183 | - private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) |
|
184 | - { |
|
185 | - $toDom = dom_import_simplexml($to); |
|
186 | - $fromDom = dom_import_simplexml($from); |
|
187 | - $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Adds the Session Variable to the given XML Request Payload |
|
192 | - * @method addSessionIdToRequest |
|
193 | - * @author PA |
|
194 | - * @date 2017-01-10 |
|
195 | - * @param \SimpleXMLElement $requestXML Base Request XML Payload |
|
196 | - */ |
|
197 | - private function addSessionIdToRequest(\SimpleXMLElement $requestXML) : \SimpleXMLElement |
|
198 | - { |
|
199 | - $requestXML->addChild('SessionID', self::$sessionId); |
|
200 | - return $requestXML; |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Setter Method for SessionID |
|
206 | - * @method setSessionId |
|
207 | - * @author PA |
|
208 | - * @date 2017-01-10 |
|
209 | - * @param string $sessionId YourMembership Session ID |
|
210 | - */ |
|
211 | - public static function setSessionId(string $sessionId) |
|
212 | - { |
|
213 | - self::$sessionId = $sessionId; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Checks if we have an active session available |
|
218 | - * @method hasSession |
|
219 | - * @author PA |
|
220 | - * @date 2017-01-11 |
|
221 | - * @return boolean |
|
222 | - */ |
|
223 | - public function hasSession() |
|
224 | - { |
|
225 | - return !is_null(self::$sessionId); |
|
226 | - } |
|
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 | + * @throws YourMembershipRequestException |
|
77 | + * @date 2017-01-09 |
|
78 | + * @param string $method YourMembership API Function Name |
|
79 | + * @param array $arguments Array of Arguments to be passed as part of the YourMembership "Call" |
|
80 | + * @return \SimpleXMLElement |
|
81 | + */ |
|
82 | + public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement |
|
83 | + { |
|
84 | + //Create Call Node |
|
85 | + $call = new \SimpleXMLElement('<Call> </Call>'); |
|
86 | + $call->addAttribute('Method', $method); |
|
87 | + |
|
88 | + //Add Arguments to the Call Node |
|
89 | + try { |
|
90 | + $call = $this->sxmlAddChildrenRecursive($call, $arguments); |
|
91 | + } catch (\Exception $e) { |
|
92 | + throw new YourMembershipRequestException($e->getMessage(), $e->getCode(), $e); |
|
93 | + } |
|
94 | + return $call; |
|
95 | + } |
|
96 | + /** |
|
97 | + * Recursively builds the array into a XML Tree |
|
98 | + * //NOTE Child arrays must be associative |
|
99 | + * @method sxmlAddChildrenRecursive |
|
100 | + * @author PA |
|
101 | + * @throws \Exception XML Parsing Exception |
|
102 | + * @date 2017-01-12 |
|
103 | + * @param \SimpleXMLElement $root Root XML Node |
|
104 | + * @param array $arguments Array of Arguments to be added to XML Node |
|
105 | + * @return \SimpleXMLElement Resulting XML Tree |
|
106 | + */ |
|
107 | + private function sxmlAddChildrenRecursive(\SimpleXMLElement $root, array $arguments) : \SimpleXMLElement |
|
108 | + { |
|
109 | + foreach ($arguments as $key => $value) { |
|
110 | + if (is_array($value)) { |
|
111 | + $child = new \SimpleXMLElement(sprintf('<%s></%s>', $key, $key)); |
|
112 | + $this->sxmlAddChildrenRecursive($child, $value); |
|
113 | + $this->sxmlAppend($root, $child); |
|
114 | + } else { |
|
115 | + $root->addChild($key, $value); |
|
116 | + } |
|
117 | + |
|
118 | + } |
|
119 | + return $root; |
|
120 | + } |
|
121 | + /** |
|
122 | + * Builds The XML Request Body for the Your Membership API Call |
|
123 | + * @method buildXMLBody |
|
124 | + * @author PA |
|
125 | + * @date 2017-01-10 |
|
126 | + * @param string $method Your Membership API Function Name |
|
127 | + * @param array $arguments Your Membership Arguments |
|
128 | + * @return \SimpleXMLElement |
|
129 | + */ |
|
130 | + public function buildXMLBody(string $method, array $arguments) : \SimpleXMLElement |
|
131 | + { |
|
132 | + $xml = $this->buildBasePayload(); // Common Envelope |
|
133 | + |
|
134 | + if ($this->isSessionRequiredForMethod($method)) { |
|
135 | + $xml = $this->addSessionIdToRequest($xml); |
|
136 | + } |
|
137 | + |
|
138 | + $callPayload = $this->createCallPayload($method, $arguments); // Specific API Call Envelope |
|
139 | + |
|
140 | + // Put Api call into common envelope |
|
141 | + $this->sxmlAppend($xml, $callPayload); |
|
142 | + |
|
143 | + return $xml; |
|
144 | + } |
|
145 | + /** |
|
146 | + * Builds a Guzzle Request Object |
|
147 | + * @method buildRequest |
|
148 | + * @author PA |
|
149 | + * @date 2017-01-11 |
|
150 | + * @param string $method YourMembership API Method |
|
151 | + * @param array $arguments YourMembership API Method Call Arguments |
|
152 | + * @return \GuzzleHttp\Psr7\Request Guzzle Request Object |
|
153 | + */ |
|
154 | + public function buildRequest(string $method, array $arguments) : \GuzzleHttp\Psr7\Request |
|
155 | + { |
|
156 | + $requestBody = $this->buildXMLBody($method, $arguments)->asXML(); |
|
157 | + return new GuzzleRequest('POST', self::BASE_URL, ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF8'], $requestBody); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Checks if Request Requires Session ID |
|
162 | + * @method isSessionRequiredForMethod |
|
163 | + * @author PA |
|
164 | + * @date 2017-01-10 |
|
165 | + * @param string $method YourMembership API Method |
|
166 | + * @return bool |
|
167 | + */ |
|
168 | + public function isSessionRequiredForMethod(string $method) : bool |
|
169 | + { |
|
170 | + //TODO Add config Logic for what API Methods require Session ID |
|
171 | + return ($method != 'Session.Create'); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Helper for Deep Copy for of $from element into $to element for SimpleXML |
|
176 | + * @method sxmlAppend |
|
177 | + * @author PA |
|
178 | + * @date 2017-01-09 |
|
179 | + * @param \SimpleXMLElement $to |
|
180 | + * @param \SimpleXMLElement $from |
|
181 | + * @return void |
|
182 | + */ |
|
183 | + private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) |
|
184 | + { |
|
185 | + $toDom = dom_import_simplexml($to); |
|
186 | + $fromDom = dom_import_simplexml($from); |
|
187 | + $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Adds the Session Variable to the given XML Request Payload |
|
192 | + * @method addSessionIdToRequest |
|
193 | + * @author PA |
|
194 | + * @date 2017-01-10 |
|
195 | + * @param \SimpleXMLElement $requestXML Base Request XML Payload |
|
196 | + */ |
|
197 | + private function addSessionIdToRequest(\SimpleXMLElement $requestXML) : \SimpleXMLElement |
|
198 | + { |
|
199 | + $requestXML->addChild('SessionID', self::$sessionId); |
|
200 | + return $requestXML; |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Setter Method for SessionID |
|
206 | + * @method setSessionId |
|
207 | + * @author PA |
|
208 | + * @date 2017-01-10 |
|
209 | + * @param string $sessionId YourMembership Session ID |
|
210 | + */ |
|
211 | + public static function setSessionId(string $sessionId) |
|
212 | + { |
|
213 | + self::$sessionId = $sessionId; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Checks if we have an active session available |
|
218 | + * @method hasSession |
|
219 | + * @author PA |
|
220 | + * @date 2017-01-11 |
|
221 | + * @return boolean |
|
222 | + */ |
|
223 | + public function hasSession() |
|
224 | + { |
|
225 | + return !is_null(self::$sessionId); |
|
226 | + } |
|
227 | 227 | |
228 | 228 | } |
@@ -48,7 +48,7 @@ |
||
48 | 48 | |
49 | 49 | $response = new Response($method, $this->client->send($request)); |
50 | 50 | |
51 | - if($response->hasError()) |
|
51 | + if ($response->hasError()) |
|
52 | 52 | { |
53 | 53 | throw new YourMembershipResponseException($response->getError(), $response->getErrorCode(), $method); |
54 | 54 | } |
@@ -48,8 +48,7 @@ |
||
48 | 48 | |
49 | 49 | $response = new Response($method, $this->client->send($request)); |
50 | 50 | |
51 | - if($response->hasError()) |
|
52 | - { |
|
51 | + if($response->hasError()) { |
|
53 | 52 | throw new YourMembershipResponseException($response->getError(), $response->getErrorCode(), $method); |
54 | 53 | } |
55 | 54 |