Completed
Push — master ( b3f24e...84a5c5 )
by Tobias
03:15
created

ArrayHydrator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 23
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A deserialize() 0 14 3
1
<?php
2
3
/*
4
 * Copyright (C) 2013-2016 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Hydrator;
11
12
use Mailgun\Exception\DeserializeException;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Serialize an HTTP response to array.
17
 *
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
class ArrayHydrator implements Hydrator
21
{
22
    /**
23
     * @param ResponseInterface $response
24
     * @param string            $class
25
     *
26
     * @return array
27
     */
28
    public function deserialize(ResponseInterface $response, $class)
29
    {
30
        $body = $response->getBody()->__toString();
31
        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
32
            throw new DeserializeException('The ArrayHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type'));
33
        }
34
35
        $content = json_decode($body, true);
36
        if (JSON_ERROR_NONE !== json_last_error()) {
37
            throw new DeserializeException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
38
        }
39
40
        return $content;
41
    }
42
}
43