JsonMessage   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isJson() 0 6 1
A decode() 0 8 2
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