Completed
Push — master ( a826f8...312899 )
by Yuichi
25s
created

JsonStream   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 21
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 16 3
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 58
    public function jsonSerialize()
16
    {
17 58
        $contents = (string) $this->getContents();
18 58
        if ($contents === '') {
19 1
            return null;
20
        }
21 57
        $decodedContents = json_decode($contents, true);
22 57
        if (json_last_error() !== JSON_ERROR_NONE) {
23 1
            throw new \RuntimeException(
24
                'Error trying to decode response: ' .
25 1
                json_last_error_msg()
26
            );
27
        }
28
29 56
        return $decodedContents;
30
    }
31
}