PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/gateways/simplify-commerce/includes/Simplify/Exceptions.php

https://gitlab.com/hunt9310/ras
PHP | 290 lines | 124 code | 46 blank | 120 comment | 12 complexity | 9727acb6bf04275ca8187531430cfe5e MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2013 - 2015 MasterCard International Incorporated
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification, are
  7. * permitted provided that the following conditions are met:
  8. *
  9. * Redistributions of source code must retain the above copyright notice, this list of
  10. * conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this list of
  12. * conditions and the following disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. * Neither the name of the MasterCard International Incorporated nor the names of its
  15. * contributors may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  20. * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  22. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  24. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /**
  29. *
  30. * Base class for all API exceptions.
  31. *
  32. */
  33. class Simplify_ApiException extends Exception
  34. {
  35. protected $errorData;
  36. protected $status;
  37. protected $errorCode;
  38. protected $reference;
  39. /**
  40. * @ignore
  41. */
  42. function __construct($message, $status = null, $errorData = null) {
  43. parent::__construct($message);
  44. $this->status = $status;
  45. $this->errorCode = null;
  46. $this->reference = null;
  47. if ($errorData != null) {
  48. $this->reference = $errorData['reference'];
  49. $this->errorData = $errorData;
  50. $error = $errorData['error'];
  51. if ($error != null) {
  52. $m = $error['message'];
  53. if ($m != null) {
  54. $this->message = $m;
  55. }
  56. $this->errorCode = $error['code'];
  57. }
  58. }
  59. }
  60. /**
  61. * Returns a map of all error data returned by the API.
  62. * @return array a map containing API error data.
  63. */
  64. function getErrorData() {
  65. return $this->errorData;
  66. }
  67. /**
  68. * Returns the HTTP status for the request.
  69. * @return string HTTP status code (or null if there is no status).
  70. */
  71. function getStatus() {
  72. return $this->status;
  73. }
  74. /**
  75. * Returns unique reference for the API error.
  76. * @return string a reference (or null if there is no reference).
  77. */
  78. function getReference() {
  79. return $this->reference;
  80. }
  81. /**
  82. * Returns an code for the API error.
  83. * @return string the error code.
  84. */
  85. function getErrorCode() {
  86. return $this->errorCode;
  87. }
  88. /**
  89. * Returns a description of the error.
  90. * @return string Description of the error.
  91. */
  92. function describe() {
  93. return get_class($this) . ": \""
  94. . $this->getMessage() . "\" (status: "
  95. . $this->getStatus() . ", error code: "
  96. . $this->getErrorCode() . ", reference: "
  97. . $this->getReference() . ")";
  98. }
  99. }
  100. /**
  101. * Exception raised when there are communication problems contacting the API.
  102. */
  103. class Simplify_ApiConnectionException extends Simplify_ApiException {
  104. /**
  105. * @ignore
  106. */
  107. function __construct($message, $status = null, $errorData = null) {
  108. parent::__construct($message, $status, $errorData);
  109. }
  110. }
  111. /**
  112. * Exception raised where there are problems authenticating a request.
  113. */
  114. class Simplify_AuthenticationException extends Simplify_ApiException {
  115. /**
  116. * @ignore
  117. */
  118. function __construct($message, $status = null, $errorData = null) {
  119. parent::__construct($message, $status, $errorData);
  120. }
  121. }
  122. /**
  123. * Exception raised when the API request contains errors.
  124. */
  125. class Simplify_BadRequestException extends Simplify_ApiException {
  126. protected $fieldErrors;
  127. /**
  128. * @ignore
  129. */
  130. function __construct($message, $status = null, $errorData = null) {
  131. parent::__construct($message, $status, $errorData);
  132. $fieldErrors = array();
  133. if ($errorData != null) {
  134. $error = $errorData['error'];
  135. if ($error != null) {
  136. $fieldErrors = $error['fieldErrors'];
  137. if ($fieldErrors != null) {
  138. $this->fieldErrors = array();
  139. foreach ($fieldErrors as $fieldError) {
  140. array_push($this->fieldErrors, new Simplify_FieldError($fieldError));
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Returns a boolean indicating whether there are any field errors.
  148. * @return boolean true if there are field errors; false otherwise.
  149. */
  150. function hasFieldErrors() {
  151. return count($this->fieldErrors) > 0;
  152. }
  153. /**
  154. * Returns a list containing all field errors.
  155. * @return array list of field errors.
  156. */
  157. function getFieldErrors() {
  158. return $this->fieldErrors;
  159. }
  160. /**
  161. * Returns a description of the error.
  162. * @return string description of the error.
  163. */
  164. function describe() {
  165. $s = parent::describe();
  166. foreach ($this->getFieldErrors() as $fieldError) {
  167. $s = $s . "\n" . (string) $fieldError;
  168. }
  169. return $s . "\n";
  170. }
  171. }
  172. /**
  173. * Represents a single error in a field of a request sent to the API.
  174. */
  175. class Simplify_FieldError {
  176. protected $field;
  177. protected $code;
  178. protected $message;
  179. /**
  180. * @ignore
  181. */
  182. function __construct($errorData) {
  183. $this->field = $errorData['field'];
  184. $this->code = $errorData['code'];
  185. $this->message = $errorData['message'];
  186. }
  187. /**
  188. * Returns the name of the field with the error.
  189. * @return string the field name.
  190. */
  191. function getFieldName() {
  192. return $this->field;
  193. }
  194. /**
  195. * Returns the code for the error.
  196. * @return string the error code.
  197. */
  198. function getErrorCode() {
  199. return $this->code;
  200. }
  201. /**
  202. * Returns a description of the error.
  203. * @return string description of the error.
  204. */
  205. function getMessage() {
  206. return $this->message;
  207. }
  208. function __toString() {
  209. return "Field error: " . $this->getFieldName() . "\"" . $this->getMessage() . "\" (" . $this->getErrorCode() . ")";
  210. }
  211. }
  212. /**
  213. * Exception when a requested object cannot be found.
  214. */
  215. class Simplify_ObjectNotFoundException extends Simplify_ApiException {
  216. /**
  217. * @ignore
  218. */
  219. function __construct($message, $status = null, $errorData = null) {
  220. parent::__construct($message, $status, $errorData);
  221. }
  222. }
  223. /**
  224. * Exception when a request was not allowed.
  225. */
  226. class Simplify_NotAllowedException extends Simplify_ApiException {
  227. /**
  228. * @ignore
  229. */
  230. function __construct($message, $status = null, $errorData = null) {
  231. parent::__construct($message, $status, $errorData);
  232. }
  233. }
  234. /**
  235. * Exception when there was a system error processing a request.
  236. */
  237. class Simplify_SystemException extends Simplify_ApiException {
  238. /**
  239. * @ignore
  240. */
  241. function __construct($message, $status = null, $errorData = null) {
  242. parent::__construct($message, $status, $errorData);
  243. }
  244. }