JsonMessage::decode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Muzzle\Messages;
4
5
trait JsonMessage
6
{
7
8
    protected $json;
9
10
    /**
11
     * Gets the body of the message.
12
     *
13
     * @return \Psr\Http\Message\StreamInterface Returns the body as a stream.
14
     */
15
    abstract public function getBody();
16
17
    /**
18
     * Check if the body of the response is JSON decodable.
19
     *
20
     * @return bool
21
     */
22
    public function isJson() : bool
23
    {
24
25
        $this->decode();
26
27
        return json_last_error() === JSON_ERROR_NONE;
28
    }
29
30
    /**
31
     * Decodes a JSON body to an array.
32
     *
33
     * @return array
34
     */
35
    public function decode() : array
36
    {
37
38
        if (! $this->json) {
39
            $this->json = json_decode($this->getBody(), true);
40
        }
41
42
        return $this->json;
43
    }
44
}
45