Passed
Pull Request — develop (#22)
by Adam
01:46
created

AbstractHydrator::getBodyContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace IBM\Watson\Common\Hydrator;
6
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * AbstractHydrator provides common protected methods other hydrators might need to use.
11
 */
12
abstract class AbstractHydrator implements HydratorInterface
13
{
14
    const HEADER_CONTENT_TYPE = 'Content-Type';
15
16
    /**
17
     * @param \Psr\Http\Message\ResponseInterface $response Response to check.
18
     *
19
     * @return bool
20
     *
21
     */
22
    protected function isJsonResponse(ResponseInterface $response): bool
23
    {
24
        return 0 === \strpos($response->getHeaderLine(self::HEADER_CONTENT_TYPE), 'application/json');
25
    }
26
27
    /**
28
     * @param \Psr\Http\Message\ResponseInterface $response Response to parse.
29
     *
30
     * @return string
31
     */
32
    protected function getBody(ResponseInterface $response): string
33
    {
34
        return $response->getBody()->__toString();
35
    }
36
37
    /**
38
     * @param \Psr\Http\Message\ResponseInterface $response Response to parse.
39
     *
40
     * @return array|null
41
     */
42
    protected function getBodyContent(ResponseInterface $response): ?array
43
    {
44
        return json_decode($this->getBody($response), true);
45
    }
46
}
47