Passed
Push — master ( 6268af...1cead5 )
by Zbigniew
02:51
created

GuzzleTransformer::transform()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0106

Importance

Changes 0
Metric Value
cc 6
nc 11
nop 1
dl 0
loc 25
ccs 14
cts 15
cp 0.9333
crap 6.0106
rs 8.8977
c 0
b 0
f 0
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 (false === $exception instanceof BadResponseException) {
31 1
            return new ApiException($exception);
32
        }
33
34
        try {
35
            /** @var BadResponseException $exception */
36 28
            $errorResponse = $exception->getResponse();
37 28
            if (null === $errorResponse) {
38
                return new ApiException($exception);
39
            }
40 28
            $errorStatusCode = $errorResponse->getStatusCode();
41 28
            $bodyString = (string) $errorResponse->getBody();
42 27
            $bodyArray = json_decode($bodyString, true);
43 27
            $errorStatusName = '';
44 27
            if (\is_array($bodyArray) && array_key_exists('error', $bodyArray)) {
45 27
                $errorStatusName = $bodyArray['error'];
46
            }
47 1
        } catch (\Exception $e) {
48 1
            return new ApiException($exception);
49
        }
50
51 27
        return $this->transformByStatusCodeAndName($exception, $errorStatusCode, $errorStatusName);
52
    }
53
}
54