RequestException::isTransient()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 0
crap 20
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
    return array(
118
      'code' =>
119 8
        static::idx($error_data, 'code', static::idx($response_data, 'code')),
120 8
      'error_subcode' => static::idx($error_data, 'error_subcode'),
121 8
      'message' => static::idx($error_data, 'message'),
122 8
      'error_user_title' => static::idx($error_data, 'error_user_title'),
123 8
      'error_user_msg' => static::idx($error_data, 'error_user_msg'),
124
      'error_blame_field_specs' =>
125 8
        static::idx(static::idx($error_data, 'error_data', array()),
126 8
          'blame_field_specs'),
127 8
      'type' => static::idx($error_data, 'type'),
128 8
    );
129
  }
130
131
  /**
132
   * Process an error payload from the Graph API and return the appropriate
133
   * exception subclass.
134
   * @param ResponseInterface $response
135
   * @return RequestException
136
   */
137 7
  public static function create(ResponseInterface $response) {
138 7
    $error_data = static::getErrorData($response);
139 7
    if (in_array(
140 7
      $error_data['error_subcode'], array(458, 459, 460, 463, 464, 467))
141 7
      || in_array($error_data['code'], array(100, 102, 190))
142 7
      || $error_data['type'] === 'OAuthException') {
143
144 1
      return new AuthorizationException($response);
145 6
    } elseif (in_array($error_data['code'], array(1, 2))) {
146
147 2
      return new ServerException($response);
148 4
    } elseif (in_array($error_data['code'], array(4, 17, 341))) {
149
150 1
      return new ThrottleException($response);
151 3
    } elseif ($error_data['code'] == 506) {
152
153 1
      return new ClientException($response);
154 2
    } elseif ($error_data['code'] == 10
155 2
      || ($error_data['code'] >= 200 && $error_data['code'] <= 299)) {
156
157 1
      return new PermissionException($response);
158
    } else {
159
160 1
      return new self($response);
161
    }
162
  }
163
164
  /**
165
   * @return int
166
   */
167 1
  public function getHttpStatusCode() {
168 1
    return $this->response->getStatusCode();
169
  }
170
171
  /**
172
   * @return int|null
173
   */
174 1
  public function getErrorSubcode() {
175 1
    return $this->errorSubcode;
176
  }
177
178
  /**
179
   * @return string|null
180
   */
181 1
  public function getErrorUserTitle() {
182 1
    return $this->errorUserTitle;
183
  }
184
185
  /**
186
   * @return string|null
187
   */
188 1
  public function getErrorUserMessage() {
189 1
    return $this->errorUserMessage;
190
  }
191
192
  /**
193
   * @return array|null
194
   */
195 1
  public function getErrorBlameFieldSpecs() {
196 1
    return $this->errorBlameFieldSpecs;
197
  }
198
199
  /**
200
   * @return bool
201
   */
202
  public function isTransient() {
203
    if ($this->getResponse() !== null) {
204
      return false;
205
    }
206
207
    $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...
208
209
    return array_key_exists('error', $body)
210
      && array_key_exists('is_transient', $body['error'])
211
      && $body['error']['is_transient'];
212
  }
213
}
214