Failed Conditions
Pull Request — master (#191)
by Emanuele
03:09
created

RequestException::create()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 20
ccs 16
cts 16
cp 1
rs 7.2765
cc 10
eloc 17
nc 6
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
29
class RequestException extends Exception
30
{
31
    /**
32
     * @var int Status code for the response causing the exception
33
     */
34
    protected $statusCode;
35
36
    /**
37
     * @var int|null
38
     */
39
    protected $errorCode;
40
41
    /**
42
     * @var int|null
43
     */
44
    protected $errorSubcode;
45
46
    /**
47
     * @var string|null
48
     */
49
    protected $errorMessage;
50
51
    /**
52
     * @var string|null
53
     */
54
    protected $errorUserTitle;
55
56
    /**
57
     * @var string|null
58
     */
59
    protected $errorUserMessage;
60
61
    /**
62
     * @var int|null
63
     */
64
    protected $errorType;
65
66
    /**
67
     * @var array|null
68
     */
69
    protected $errorBlameFieldSpecs;
70
71
    /**
72
     * @param array $response_data The response from the Graph API
73
     * @param int $status_code
74
     */
75 8
    public function __construct(
76
        array $response_data,
77
        $status_code
78
    ) {
79 8
        $this->statusCode = $status_code;
80 8
        $error_data = static::getErrorData($response_data);
81
82 8
        parent::__construct($error_data['message'], $error_data['code']);
83
84 8
        $this->errorSubcode = $error_data['error_subcode'];
85 8
        $this->errorUserTitle = $error_data['error_user_title'];
86 8
        $this->errorUserMessage = $error_data['error_user_msg'];
87 8
        $this->errorBlameFieldSpecs = $error_data['error_blame_field_specs'];
88 8
    }
89
90
    /**
91
     * @param array $array
92
     * @param string|int $key
93
     * @param mixed $default
94
     * @return mixed
95
     */
96 8
    protected static function idx(array $array, $key, $default = null)
97
    {
98 8
        return array_key_exists($key, $array)
99 8
            ? $array[$key]
100 8
            : $default;
101
    }
102
103
    /**
104
     * @param array $response_data
105
     * @return array
106
     */
107 8
    protected static function getErrorData(array $response_data)
108
    {
109 8
        $error_data = static::idx($response_data, 'error', array());
110
111
        return array(
112
            'code' =>
113 8
                static::idx($error_data, 'code', static::idx($response_data, 'code')),
114 8
            'error_subcode' => static::idx($error_data, 'error_subcode'),
115 8
            'message' => static::idx($error_data, 'message'),
116 8
            'error_user_title' => static::idx($error_data, 'error_user_title'),
117 8
            'error_user_msg' => static::idx($error_data, 'error_user_msg'),
118 8
            'error_blame_field_specs' => static::idx(
119 8
                static::idx($error_data, 'error_data', array()),
120
                'blame_field_specs'
121 8
            ),
122 8
            'type' => static::idx($error_data, 'type'),
123 8
        );
124
    }
125
126
    /**
127
     * Process an error payload from the Graph API and return the appropriate
128
     * exception subclass.
129
     * @param array $response_data the decoded response from the Graph API
130
     * @param int $status_code the HTTP response code
131
     * @return RequestException
132
     */
133 7
    public static function create(array $response_data, $status_code)
134
    {
135 7
        $error_data = static::getErrorData($response_data);
136 7
        if (in_array($error_data['error_subcode'], array(458, 459, 460, 463, 464, 467))
137 7
            || in_array($error_data['code'], array(100, 102, 190))
138 7
            || $error_data['type'] === 'OAuthException') {
139 1
            return new AuthorizationException($response_data, $status_code);
140 6
        } elseif (in_array($error_data['code'], array(1, 2))) {
141 2
            return new ServerException($response_data, $status_code);
142 4
        } elseif (in_array($error_data['code'], array(4, 17, 341))) {
143 1
            return new ThrottleException($response_data, $status_code);
144 3
        } elseif ($error_data['code'] == 506) {
145 1
            return new ClientException($response_data, $status_code);
146 2
        } elseif ($error_data['code'] == 10
147 2
            || ($error_data['code'] >= 200 && $error_data['code'] <= 299)) {
148 1
            return new PermissionException($response_data, $status_code);
149
        } else {
150 1
            return new self($response_data, $status_code);
151
        }
152
    }
153
154
    /**
155
     * @return int
156
     */
157 1
    public function getHttpStatusCode()
158
    {
159 1
        return $this->statusCode;
160
    }
161
162
    /**
163
     * @return int|null
164
     */
165 1
    public function getErrorSubcode()
166
    {
167 1
        return $this->errorSubcode;
168
    }
169
170
    /**
171
     * @return string|null
172
     */
173 1
    public function getErrorUserTitle()
174
    {
175 1
        return $this->errorUserTitle;
176
    }
177
178
    /**
179
     * @return string|null
180
     */
181 1
    public function getErrorUserMessage()
182
    {
183 1
        return $this->errorUserMessage;
184
    }
185
186
    /**
187
     * @return array|null
188
     */
189 1
    public function getErrorBlameFieldSpecs()
190
    {
191 1
        return $this->errorBlameFieldSpecs;
192
    }
193
}
194