Completed
Push — master ( d02ce0...14668b )
by Zbigniew
14:57
created

AbstractPsrResponseTransformer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 56
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A transformToPsrResponse() 0 4 1
A transformToPsrBody() 0 4 1
A transformToStringBody() 0 4 1
A transformToArrayBody() 0 4 1
A isSupportedResponseFormat() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the zibios/wrike-php-library 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\WrikePhpLibrary\Transformer\Response\Psr;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\StreamInterface;
16
use Zibios\WrikePhpLibrary\Enum\Api\ResponseFormatEnum;
17
use Zibios\WrikePhpLibrary\Transformer\ResponseTransformerInterface;
18
19
/**
20
 * Abstract Response Transformer for PSR Response from HTTP Client.
21
 */
22
abstract class AbstractPsrResponseTransformer implements ResponseTransformerInterface
23
{
24
    /**
25
     * @param string $responseFormat
26
     *
27
     * @return bool
28
     */
29 4
    public function isSupportedResponseFormat($responseFormat)
30
    {
31 4
        return $responseFormat === ResponseFormatEnum::PSR_RESPONSE;
32
    }
33
34
    /**
35
     * @param ResponseInterface $response
36
     *
37
     * @return ResponseInterface
38
     */
39 90
    protected function transformToPsrResponse(ResponseInterface $response)
40
    {
41 90
        return $response;
42
    }
43
44
    /**
45
     * @param ResponseInterface $response
46
     *
47
     * @return StreamInterface
48
     */
49 88
    protected function transformToPsrBody(ResponseInterface $response)
50
    {
51 88
        return $this->transformToPsrResponse($response)->getBody();
52
    }
53
54
    /**
55
     * @param ResponseInterface $response
56
     *
57
     * @throws \RuntimeException
58
     *
59
     * @return string
60
     */
61 86
    protected function transformToStringBody(ResponseInterface $response)
62
    {
63 86
        return $this->transformToPsrBody($response)->getContents();
64
    }
65
66
    /**
67
     * @param ResponseInterface $response
68
     *
69
     * @throws \RuntimeException
70
     *
71
     * @return array
72
     */
73 84
    protected function transformToArrayBody(ResponseInterface $response)
74
    {
75 84
        return json_decode($this->transformToStringBody($response), true);
76
    }
77
}
78