Completed
Push — master ( 837ea5...a962f3 )
by
unknown
04:34
created

RequestException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.2963

Importance

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