Completed
Push — master ( a06a42...8f4f82 )
by Zbigniew
03:40
created

GuzzleTransformer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 30
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B transform() 0 22 5
1
<?php
2
3
/*
4
 * This file is part of the zibios/wrike-php-guzzle package.
5
 *
6
 * (c) Zbigniew Ślązak
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zibios\WrikePhpGuzzle\Transformer\ApiException;
13
14
use GuzzleHttp\Exception\BadResponseException;
15
use Zibios\WrikePhpLibrary\Exception\Api\ApiException;
16
use Zibios\WrikePhpLibrary\Transformer\ApiException\AbstractApiExceptionTransformer;
17
18
/**
19
 * Guzzle Exception Transformer.
20
 */
21
class GuzzleTransformer extends AbstractApiExceptionTransformer
22
{
23
    /**
24
     * @param \Exception $exception
25
     *
26
     * @return \Exception|ApiException
27
     */
28 29
    public function transform(\Exception $exception)
29
    {
30 29
        if ($exception instanceof BadResponseException === false) {
31 1
            return new ApiException($exception);
32
        }
33
34
        try {
35
            /** @var BadResponseException $exception */
36 28
            $errorResponse = $exception->getResponse();
37 28
            $errorStatusCode = $errorResponse->getStatusCode();
38 28
            $bodyString = (string) $errorResponse->getBody();
39 27
            $bodyArray = json_decode($bodyString, true);
40 27
            $errorStatusName = '';
41 27
            if (is_array($bodyArray) && array_key_exists('error', $bodyArray)) {
42 27
                $errorStatusName = $bodyArray['error'];
43
            }
44 1
        } catch (\Exception $e) {
45 1
            return new ApiException($exception);
46
        }
47
48 27
        return $this->transformByStatusCodeAndName($exception, $errorStatusCode, $errorStatusName);
49
    }
50
}
51