ExceptionFactory::createThrowable()   B
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 13
nc 10
nop 1
1
<?php
2
namespace KWTClient\Exception;
3
4
use GuzzleHttp\Exception\ClientException;
5
6
class ExceptionFactory
7
{
8
    /**
9
     * @param ClientException $clientException
10
     *
11
     * @return ApiException | SearchLimitException
12
     */
13
    public static function createThrowable(ClientException $clientException)
14
    {
15
        $data = \GuzzleHttp\json_decode($clientException->getResponse()->getBody()->getContents());
16
17
        $code = 0;
18
        $message = '';
19
20
        if (!empty($data->error)) {
21
            $error = $data->error;
22
            if (!empty($error->code)) {
23
                $code = $error->code;
24
            }
25
26
            if (!empty($error->message)) {
27
                $message = $error->message;
28
            }
29
        }
30
31
        if ($code == SearchLimitException::EXCEPTION_CODE) {
32
            return new SearchLimitException($message);
33
        }
34
        
35
        return new ApiException($message, $code);
36
    }
37
}
38