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

WrikeTransformer::transform()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 14
nc 9
nop 1
crap 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