Passed
Push — master ( 881081...f99eff )
by Zbigniew
02:21
created

WrikeTransformer   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 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 30
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

1 Method

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