Completed
Push — master ( 039bdb...90d49c )
by Yuichi
11s
created

JsonStream::jsonSerialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.576

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 6
cts 10
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 0
crap 3.576
1
<?php
2
3
namespace CybozuHttp\Middleware;
4
5
use GuzzleHttp\Psr7\StreamDecoratorTrait;
6
use Psr\Http\Message\StreamInterface;
7
8
/**
9
 * @author ochi51 <[email protected]>
10
 */
11
class JsonStream implements StreamInterface, \JsonSerializable
12
{
13
    use StreamDecoratorTrait;
14
15 49
    public function jsonSerialize()
16
    {
17 49
        $contents = (string) $this->getContents();
18 49
        if ($contents === '') {
19
            return null;
20
        }
21 49
        $decodedContents = json_decode($contents, true);
22 49
        if (json_last_error() !== JSON_ERROR_NONE) {
23
            throw new \RuntimeException(
24
                'Error trying to decode response: ' .
25
                json_last_error_msg()
26
            );
27
        }
28
29 49
        return $decodedContents;
30
    }
31
}