Payload   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 37.93%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 110
ccs 11
cts 29
cp 0.3793
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getData() 0 4 1
A toJson() 0 4 1
A offsetGet() 0 4 2
A jsonSerialize() 0 4 1
A __toString() 0 4 1
A fromJson() 0 10 3
A offsetSet() 0 8 2
A offsetExists() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
namespace Slack;
3
4
/**
5
 * Stores incoming or outgoing message data for a Slack API call.
6
 */
7
class Payload implements \ArrayAccess, \JsonSerializable
8
{
9
    /**
10
     * @var array The response data.
11
     */
12
    protected $data;
13
14
    /**
15
     * Creates a response object from a JSON message.
16
     *
17
     * @param string $json A JSON string.
18
     *
19
     * @return Response The parsed response.
20
     */
21 11
    public static function fromJson($json)
22
    {
23 11
        $data = json_decode((string)$json, true);
24
25 11
        if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) {
26
            throw new \UnexpectedValueException('Invalid JSON message.');
27
        }
28
29 11
        return new static($data);
30
    }
31
32
    /**
33
     * Creates a new payload object.
34
     *
35
     * @param array $data The payload data.
36
     */
37 11
    public function __construct($data)
38
    {
39 11
        $this->data = $data;
40 11
    }
41
42
    /**
43
     * Gets the payload data.
44
     *
45
     * @return array The payload data.
46
     */
47
    public function getData()
48
    {
49
        return $this->data;
50
    }
51
52
    /**
53
     * Serializes the payload to a JSON message.
54
     *
55
     * @return string A JSON message.
56
     */
57
    public function toJson()
58
    {
59
        return json_encode($this->data, true);
60
    }
61
62
    /**
63
     * @param mixed $offset
64
     * @param mixed $value
65
     */
66
    public function offsetSet($offset, $value)
67
    {
68
        if (is_null($offset)) {
69
            $this->data[] = $value;
70
        } else {
71
            $this->data[$offset] = $value;
72
        }
73
    }
74
75
    /**
76
     * @param mixed $offset
77
     * @return bool
78
     */
79 11
    public function offsetExists($offset)
80
    {
81 11
        return isset($this->data[$offset]);
82
    }
83
84
    /**
85
     * @param mixed $offset
86
     */
87
    public function offsetUnset($offset)
88
    {
89
        unset($this->data[$offset]);
90
    }
91
92
    /**
93
     * @param mixed $offset
94
     * @return null
95
     */
96 11
    public function offsetGet($offset)
97
    {
98 11
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function jsonSerialize()
105
    {
106
        return $this->data;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function __toString()
113
    {
114
        return $this->toJson();
115
    }
116
}
117