exception.php (2577B)
1 <?php 2 3 namespace Squareup; 4 5 class Exception extends \Exception { 6 const ERR_CODE_ACCESS_TOKEN_REVOKED = 'ACCESS_TOKEN_REVOKED'; 7 const ERR_CODE_ACCESS_TOKEN_EXPIRED = 'ACCESS_TOKEN_EXPIRED'; 8 9 private $config; 10 private $log; 11 private $language; 12 private $errors; 13 private $isCurlError; 14 15 private $overrideFields = array( 16 'billing_address.country', 17 'shipping_address.country', 18 'email_address', 19 'phone_number' 20 ); 21 22 public function __construct($registry, $errors, $is_curl_error = false) { 23 $this->errors = $errors; 24 $this->isCurlError = $is_curl_error; 25 $this->config = $registry->get('config'); 26 $this->log = $registry->get('log'); 27 $this->language = $registry->get('language'); 28 29 $message = $this->concatErrors(); 30 31 if ($this->config->get('config_error_log')) { 32 $this->log->write($message); 33 } 34 35 parent::__construct($message); 36 } 37 38 public function isCurlError() { 39 return $this->isCurlError; 40 } 41 42 public function isAccessTokenRevoked() { 43 return $this->errorCodeExists(self::ERR_CODE_ACCESS_TOKEN_REVOKED); 44 } 45 46 public function isAccessTokenExpired() { 47 return $this->errorCodeExists(self::ERR_CODE_ACCESS_TOKEN_EXPIRED); 48 } 49 50 protected function errorCodeExists($code) { 51 if (is_array($this->errors)) { 52 foreach ($this->errors as $error) { 53 if ($error['code'] == $code) { 54 return true; 55 } 56 } 57 } 58 59 return false; 60 } 61 62 protected function overrideError($field) { 63 return $this->language->get('squareup_override_error_' . $field); 64 } 65 66 protected function parseError($error) { 67 if (!empty($error['field']) && in_array($error['field'], $this->overrideFields)) { 68 return $this->overrideError($error['field']); 69 } 70 71 $message = $error['detail']; 72 73 if (!empty($error['field'])) { 74 $message .= sprintf($this->language->get('squareup_error_field'), $error['field']); 75 } 76 77 return $message; 78 } 79 80 protected function concatErrors() { 81 $messages = array(); 82 83 if (is_array($this->errors)) { 84 foreach ($this->errors as $error) { 85 $messages[] = $this->parseError($error); 86 } 87 } else { 88 $messages[] = $this->errors; 89 } 90 91 return implode(' ', $messages); 92 } 93 }