Completed
Pull Request — develop (#22)
by Adam
01:21
created

AbstractHydrator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getBody() 0 3 1
A getBodyContent() 0 3 1
A isJsonResponse() 0 3 1
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 \strpos($response->getHeaderLine(self::HEADER_CONTENT_TYPE), 'application/json') === 0;
25
    }
26
27
    /**
28
     * @param \Psr\Http\Message\ResponseInterface $response Response to parse.
29
     *
30
     * @return string
31
     *
32
     */
33
    protected function getBody(ResponseInterface $response): string
34
    {
35
        return $response->getBody()->__toString();
36
    }
37
38
    /**
39
     * @param \Psr\Http\Message\ResponseInterface $response Response to parse.
40
     *
41
     * @return array|null
42
     *
43
     */
44
    protected function getBodyContent(ResponseInterface $response): ?array
45
    {
46
        return json_decode($this->getBody($response), true);
47
    }
48
}
49