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

RestResponse   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 45
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A noContent() 0 3 1
A createForbidden() 0 3 1
A exception() 0 7 2
A __construct() 0 5 1
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