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

ArrayHydrator::deserialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
crap 12
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