ArrayHydrator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 22
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace IBM\Watson\Common\Hydrator;
6
7
use IBM\Watson\Common\Exception\HydrationException;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * The ArrayHydrator will hydrate a json response into an associative array.
12
 */
13
class ArrayHydrator extends AbstractHydrator
14
{
15
    /**
16
     * @param \Psr\Http\Message\ResponseInterface $response Response to hydrate.
17
     * @param string|null                         $class    Class to hydrate to.
18
     *
19
     * @return array
20
     *
21
     * @throws \IBM\Watson\Common\Exception\HydrationException
22
     * @throws \IBM\Watson\Common\Exception\JsonException
23
     */
24
    public function hydrate(ResponseInterface $response, string $class = null): array
25
    {
26
        if (!$this->isJsonResponse($response)) {
27
            $message = 'The ArrayHydrator cannot hydrate a response with Content-Type: ';
28
29
            throw new HydrationException($message.$response->getHeaderLine(self::HEADER_CONTENT_TYPE));
30
        }
31
32
        $body = $this->getBodyContent($response);
33
34
        return $body;
35
    }
36
}
37