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

AbstractHydrator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isJsonResponse() 0 3 1
A getBody() 0 3 1
A getBodyContent() 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 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