IsResponse::setStatusCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Meare\Juggler\Imposter\Stub\Response;
4
5
6
class IsResponse implements IResponse
7
{
8
    const MODE_TEXT = 'text';
9
    const MODE_BINARY = 'binary';
10
11
    /**
12
     * @var string
13
     */
14
    private $type = self::TYPE_IS;
15
16
    /**
17
     * @var int
18
     */
19
    private $statusCode;
20
21
    /**
22
     * @var array
23
     */
24
    private $headers = [];
25
26
    /**
27
     * Starting from Juggler 1.4.3 inline JSON is allowed as http/s response bodies
28
     *
29
     * @var string|array
30
     */
31
    private $body;
32
33
    /**
34
     * @var string
35
     */
36
    private $mode = self::MODE_TEXT;
37
38
    /**
39
     * @param int          $status_code
40
     * @param array        $headers
41
     * @param string|array $body
42
     * @param string       $mode
43
     */
44 28
    public function __construct($status_code = 200, array $headers = [], $body = null, $mode = null)
45
    {
46 28
        $this->setStatusCode($status_code);
47 28
        $this->setHeaders($headers);
48 28
        if (null !== $body) {
49 6
            $this->setBody($body);
50 5
        }
51 27
        if (null !== $mode) {
52 2
            $this->setMode($mode);
53 2
        }
54 27
    }
55
56
    /**
57
     * @param array $contract
58
     * @return self
59
     */
60 3
    public static function createFromContract($contract)
61
    {
62 3
        if (!isset($contract['statusCode'])) {
63 1
            throw new \InvalidArgumentException("Cannot create IsResponse from contract: Invalid contract; 'statusCode' field does not exists");
64
        }
65
66 2
        return new self(
67 2
            $contract['statusCode'],
68 2
            isset($contract['headers']) ? $contract['headers'] : [],
69 2
            isset($contract['body']) ? $contract['body'] : null,
70 2
            isset($contract['_mode']) ? $contract['_mode'] : null
71 2
        );
72
    }
73
74
    /**
75
     * @param callable $callback
76
     */
77 2
    public function modifyBody(callable $callback)
78
    {
79 2
        $body = $callback($this->getBody());
80 2
        if (null === $body) {
81 1
            throw new \LogicException('Expected new body as return value, got null');
82
        }
83 1
        $this->setBody($body);
84 1
    }
85
86
    /**
87
     * @return array|string mountebank 1.4.3: You can now use inline JSON for http/s response bodies rather than a
88
     *                      string.
89
     */
90 8
    public function getBody()
91
    {
92 8
        return $this->body;
93
    }
94
95
    /**
96
     * @param array|string $body
97
     */
98 6
    public function setBody($body)
99
    {
100 6
        if (!is_string($body) && !is_array($body)) {
101 1
            throw new \InvalidArgumentException('Unable to set IsResponse body; Expected body to be string or array, '
102 1
                . 'got ' . gettype($body));
103
        }
104 5
        $this->body = $body;
105 5
    }
106
107
    /**
108
     * @return array
109
     */
110 5
    public function jsonSerialize()
111
    {
112
        return [
113 5
            IResponse::TYPE_IS => [
114 5
                'statusCode' => $this->getStatusCode(),
115 5
                'headers'    => $this->getHeaders(),
116 5
                'body'       => $this->getBody(),
117 5
                '_mode'      => $this->getMode(),
118 5
            ],
119 5
        ];
120
    }
121
122
    /**
123
     * @return int
124
     */
125 7
    public function getStatusCode()
126
    {
127 7
        return $this->statusCode;
128
    }
129
130
    /**
131
     * @param int $status_code
132
     */
133 28
    public function setStatusCode($status_code)
134
    {
135 28
        $this->statusCode = $status_code;
136 28
    }
137
138
    /**
139
     * @return array
140
     */
141 8
    public function getHeaders()
142
    {
143 8
        return $this->headers;
144
    }
145
146
    /**
147
     * TODO headers with same name
148
     *
149
     * @param array $headers ['name' => 'value', ..]
150
     */
151 28
    public function setHeaders(array $headers)
152
    {
153 28
        foreach ($headers as $name => $value) {
154 6
            $this->setHeader($name, $value);
155 28
        }
156 28
    }
157
158
    /**
159
     * @return string
160
     */
161 7
    public function getMode()
162
    {
163 7
        return $this->mode;
164
    }
165
166
    /**
167
     * @param string $mode
168
     */
169 4
    public function setMode($mode)
170
    {
171 4 View Code Duplication
        if (!in_array($mode, [self::MODE_TEXT, self::MODE_BINARY])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172 1
            throw new \InvalidArgumentException("Unable to set IsResponse mode; Invalid mode: '$mode'");
173
        }
174 3
        $this->mode = $mode;
175 3
    }
176
177
    /**
178
     * @return string
179
     */
180 3
    public function getType()
181
    {
182 3
        return $this->type;
183
    }
184
185
    /**
186
     * @param string $name
187
     * @param string $value
188
     */
189 6
    public function setHeader($name, $value)
190
    {
191 6
        if (!is_string($name)) {
192 1
            throw new \InvalidArgumentException(
193
                'Unable to set IsResponse headers; Header name expected be string, '
194 1
                . gettype($name) . " received ('" . (string)$name . "')"
195 1
            );
196
        }
197 5
        $this->headers[$name] = $value;
198 5
    }
199
200
    /**
201
     * @param string $name
202
     * @return mixed|null
203
     */
204 1
    public function getHeader($name)
205
    {
206 1
        return isset($this->headers[$name]) ? $this->headers[$name] : null;
207
    }
208
}
209