Passed
Branch dev (8e1e05)
by Alex
03:51
created

HttpExceptionAbstract::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * Codeburner Framework.
5
 *
6
 * @author Alex Rohleder <[email protected]>
7
 * @copyright 2016 Alex Rohleder
8
 * @license http://opensource.org/licenses/MIT
9
 */
10
11
namespace Codeburner\Router\Exceptions\Http;
12
13
use Exception;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * Class HttpExceptionAbstract
18
 *
19
 * @author Alex Rohleder <[email protected]>
20
 */
21
22
abstract class HttpExceptionAbstract extends Exception
23
{
24
25
    public function getResponse(ResponseInterface $response)
26
    {
27
        return $response->withStatus($this->code, $this->message);
28
    }
29
30
    public function getJsonResponse(ResponseInterface $response)
31
    {
32
        $response->withAddedHeader("content-type", "application/json");
33
34
        if ($response->getBody()->isWritable()) {
35
            $response->getBody()->write(json_encode(["status-code" => $this->code, "reason-phrase" => $this->message]));
36
        }
37
38
        return $this->getResponse($response);
39
    }
40
41
}