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

GuzzleTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 33
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B transform() 0 25 6
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