Spike::getHeader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the slince/spike package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Spike\Common\Protocol;
13
14
use Spike\Common\Exception\BadRequestException;
15
16
class Spike implements SpikeInterface
17
{
18
    /**
19
     * The action.
20
     *
21
     * @var string
22
     */
23
    protected $action;
24
25
    /**
26
     * Array of custom headers.
27
     *
28
     * @var array
29
     */
30
    protected $headers = [];
31
32
    /**
33
     * @var mixed
34
     */
35
    protected $body;
36
37
    /**
38
     * The global headers.
39
     *
40
     * @var array
41
     */
42
    protected static $globalHeaders = [];
43
44
    /**
45
     * Spike constructor.
46
     *
47
     * @param string $action
48
     * @param mixed  $body
49
     * @param array  $headers
50
     */
51
    public function __construct($action, $body = null, $headers = [])
52
    {
53
        $this->action = $action;
54
        $this->body = $body;
55
        $this->headers = $headers;
56
    }
57
58
    public function __toString()
59
    {
60
        return $this->toString();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function setAction($action)
67
    {
68
        $this->action = $action;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getAction()
75
    {
76
        return $this->action;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function setHeaders(array $headers)
83
    {
84
        $this->headers = $headers;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getHeaders()
91
    {
92
        return $this->headers;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function setHeader($name, $value)
99
    {
100
        $this->headers[$name] = $value;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getHeader($name)
107
    {
108
        return isset($this->headers[$name]) ? $this->headers[$name] : null;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function toString()
115
    {
116
        $data = [
117
            'action' => $this->action,
118
            'headers' => array_merge($this->headers, static::$globalHeaders),
119
            'body' => $this->body,
120
        ];
121
122
        return (string)json_encode($data);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function setBody($body)
129
    {
130
        $this->body = $body;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function getBody()
137
    {
138
        return $this->body;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public static function fromString($string)
145
    {
146
        $parsed = json_decode($string, true);
147
        if (json_last_error() || !isset($headers['action'])) {
0 ignored issues
show
Bug introduced by
The variable $headers seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
148
            throw new BadRequestException('Bad spike protocol message"');
149
        }
150
151
        return new static($parsed['action'], $parsed['body'], $parsed['headers']);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public static function fromArray($array)
158
    {
159
        if (!isset($array['action'])) {
160
            throw new BadRequestException('Bad spike protocol message"');
161
        }
162
163
        return new static($array['action'], $array['body'], $array['headers']);
164
    }
165
166
    /**
167
     * Sets a global header.
168
     *
169
     * @param string $name
170
     * @param string $value
171
     */
172
    public static function setGlobalHeader($name, $value)
173
    {
174
        static::$globalHeaders[$name] = $value;
175
    }
176
}