Completed
Pull Request — master (#406)
by
unknown
16:29
created

RequestException   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 83.58%

Importance

Changes 0
Metric Value
wmc 26
lcom 2
cbo 7
dl 0
loc 188
ccs 56
cts 67
cp 0.8358
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A idx() 0 5 2
A __construct() 0 11 1
A getResponse() 0 3 1
B getErrorData() 0 24 3
D create() 0 26 10
A getHttpStatusCode() 0 3 1
A getErrorSubcode() 0 3 1
A getErrorUserTitle() 0 3 1
A getErrorUserMessage() 0 3 1
A getErrorBlameFieldSpecs() 0 3 1
A isTransient() 0 11 4
1
<?php
2
/**
3
 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
namespace FacebookAds\Http\Exception;
26
27
use FacebookAds\Exception\Exception;
28
use FacebookAds\Http\ResponseInterface;
29
30
class RequestException extends Exception {
31
32
  /**
33
   * @var ResponseInterface|null
34
   */
35
  protected $response;
36
37
  /**
38
   * @var int|null
39
   */
40
  protected $errorCode;
41
42
  /**
43
   * @var int|null
44
   */
45
  protected $errorSubcode;
46
47
  /**
48
   * @var string|null
49
   */
50
  protected $errorMessage;
51
52
  /**
53
   * @var string|null
54
   */
55
  protected $errorUserTitle;
56
57
  /**
58
   * @var string|null
59
   */
60
  protected $errorUserMessage;
61
62
  /**
63
   * @var int|null
64
   */
65
  protected $errorType;
66
67
  /**
68
   * @var array|null
69
   */
70
  protected $errorBlameFieldSpecs;
71
72
  /**
73
   * @param ResponseInterface $response
74
   */
75 8
  public function __construct(ResponseInterface $response) {
76 8
    $this->response = $response;
77 8
    $error_data = static::getErrorData($response);
78
79 8
    parent::__construct($error_data['message'], $error_data['code']);
80
81 8
    $this->errorSubcode = $error_data['error_subcode'];
82 8
    $this->errorUserTitle = $error_data['error_user_title'];
83 8
    $this->errorUserMessage = $error_data['error_user_msg'];
84 8
    $this->errorBlameFieldSpecs = $error_data['error_blame_field_specs'];
85 8
  }
86
87
  /**
88
   * @return ResponseInterface|null
89
   */
90
  public function getResponse() {
91
    return $this->response;
92
  }
93
94
  /**
95
   * @param array $array
96
   * @param string|int $key
97
   * @param mixed $default
98
   * @return mixed
99
   */
100 8
  protected static function idx(array $array, $key, $default = null) {
101 8
    return array_key_exists($key, $array)
102 8
      ? $array[$key]
103 8
      : $default;
104
  }
105
106
  /**
107
   * @param ResponseInterface $response
108
   * @return array
109
   */
110 8
  protected static function getErrorData(ResponseInterface $response) {
111 8
    $response_data = $response->getContent();
112 8
    if (is_null($response_data)) {
113 1
      $response_data = array();
114 1
    }
115 8
    $error_data = static::idx($response_data, 'error', array());
116
117 8
    if (is_string(static::idx($error_data, 'error_data'))) {
118
      $error_data["error_data"] = json_decode(stripslashes(static::idx($error_data, 'error_data')), true);
119
    }
120
121
    return array(
122
      'code' =>
123 8
        static::idx($error_data, 'code', static::idx($response_data, 'code')),
124 8
      'error_subcode' => static::idx($error_data, 'error_subcode'),
125 8
      'message' => static::idx($error_data, 'message'),
126 8
      'error_user_title' => static::idx($error_data, 'error_user_title'),
127 8
      'error_user_msg' => static::idx($error_data, 'error_user_msg'),
128
      'error_blame_field_specs' =>
129 8
        static::idx(static::idx($error_data, 'error_data', array()),
130 8
          'blame_field_specs'),
131 8
      'type' => static::idx($error_data, 'type'),
132 8
    );
133
  }
134
135
  /**
136
   * Process an error payload from the Graph API and return the appropriate
137
   * exception subclass.
138
   * @param ResponseInterface $response
139
   * @return RequestException
140
   */
141 7
  public static function create(ResponseInterface $response) {
142 7
    $error_data = static::getErrorData($response);
143 7
    if (in_array(
144 7
      $error_data['error_subcode'], array(458, 459, 460, 463, 464, 467))
145 7
      || in_array($error_data['code'], array(100, 102, 190))
146 7
      || $error_data['type'] === 'OAuthException') {
147
148 1
      return new AuthorizationException($response);
149 6
    } elseif (in_array($error_data['code'], array(1, 2))) {
150
151 2
      return new ServerException($response);
152 4
    } elseif (in_array($error_data['code'], array(4, 17, 341))) {
153
154 1
      return new ThrottleException($response);
155 3
    } elseif ($error_data['code'] == 506) {
156
157 1
      return new ClientException($response);
158 2
    } elseif ($error_data['code'] == 10
159 2
      || ($error_data['code'] >= 200 && $error_data['code'] <= 299)) {
160
161 1
      return new PermissionException($response);
162
    } else {
163
164 1
      return new self($response);
165
    }
166
  }
167
168
  /**
169
   * @return int
170
   */
171 1
  public function getHttpStatusCode() {
172 1
    return $this->response->getStatusCode();
173
  }
174
175
  /**
176
   * @return int|null
177
   */
178 1
  public function getErrorSubcode() {
179 1
    return $this->errorSubcode;
180
  }
181
182
  /**
183
   * @return string|null
184
   */
185 1
  public function getErrorUserTitle() {
186 1
    return $this->errorUserTitle;
187
  }
188
189
  /**
190
   * @return string|null
191
   */
192 1
  public function getErrorUserMessage() {
193 1
    return $this->errorUserMessage;
194
  }
195
196
  /**
197
   * @return array|null
198
   */
199 1
  public function getErrorBlameFieldSpecs() {
200 1
    return $this->errorBlameFieldSpecs;
201
  }
202
203
  /**
204
   * @return bool
205
   */
206
  public function isTransient() {
207
    if ($this->getResponse() !== null) {
208
      return false;
209
    }
210
211
    $body = $this->getResponse()->getBody();
0 ignored issues
show
Bug introduced by
The method getBody cannot be called on $this->getResponse() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
212
213
    return array_key_exists('error', $body)
214
      && array_key_exists('is_transient', $body['error'])
215
      && $body['error']['is_transient'];
216
  }
217
}
218