Passed
Push — master ( 383b45...c02d9e )
by Pavel
01:44
created

RestResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 3
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest;
2
3
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
4
use Pz\Doctrine\Rest\Exceptions\RestException;
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
7
class RestResponse extends JsonResponse
8
{
9
    /**
10
     * @return static
11
     */
12 1
    public static function noContent()
13
    {
14 1
        return static::create(null, static::HTTP_NO_CONTENT);
15
    }
16
17
    /**
18
     * @return static
19
     */
20 1
    public static function createForbidden()
21
    {
22 1
        return static::create(null, static::HTTP_FORBIDDEN);
23
    }
24
25
    /**
26
     * @param \Error|\Exception|RestException $exception
27
     *
28
     * @return RestResponse
29
     * @throws \Error|\Exception|RestException
30
     */
31 3
    public static function exception(\Exception $exception)
32
    {
33 3
        if (!$exception instanceof RestException) {
34 1
            $exception = RestException::createFromException($exception);
35
        }
36
37 3
        return RestResponse::create(['errors' => $exception->errors()], $exception->getCode());
38
    }
39
40
    /**
41
     * RestResponse constructor.
42
     *
43
     * @param mixed|null $data
44
     * @param int        $status
45
     * @param array      $headers
46
     */
47 15
    public function __construct($data, $status, array $headers = [])
48
    {
49 15
        $headers['Content-Type'] = RestRequestContract::JSON_API_CONTENT_TYPE;
50
51 15
        parent::__construct($data, $status, $headers, false);
52 15
    }
53
}
54