Completed
Push — master ( a5189e...da7714 )
by Pavel
02:31
created

RestResponseFactory::noContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest;
2
3
use League\Fractal\Resource\ResourceInterface;
4
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
5
use Pz\Doctrine\Rest\Fractal\JsonApiSerializer;
6
use Pz\Doctrine\Rest\Fractal\Manager;
7
use Pz\Doctrine\Rest\Fractal\ScopeFactory;
8
9
class RestResponseFactory
10
{
11
    /**
12
     * Return configured fractal by request format.
13
     *
14
     * @param RestRequestContract $request
15
     *
16
     * @return Manager
17
     */
18 18
    protected static function fractal(RestRequestContract $request)
19
    {
20 18
        $fractal = new Manager(new ScopeFactory(), $request);
21 18
        $fractal->setSerializer(new JsonApiSerializer($request));
22
23 18
        if ($includes = $request->getInclude()) {
24 5
            $fractal->parseIncludes($includes);
25
        }
26
27 18
        if ($excludes = $request->getExclude()) {
28 1
            $fractal->parseExcludes($excludes);
29
        }
30
31 18
        if ($fields = $request->getFields()) {
32 5
            $fractal->parseFieldsets($fields);
33
        }
34
35 18
        return $fractal;
36
    }
37
38
    /**
39
     * @param RestRequestContract    $request
40
     * @param ResourceInterface|null $resource
41
     * @param int                    $httStatus
42
     * @param array                  $headers
43
     *
44
     * @return RestResponse
45
     */
46 18
    public function resource(
47
        RestRequestContract $request,
48
        ResourceInterface   $resource = null,
49
        int                 $httStatus = RestResponse::HTTP_OK,
50
        array               $headers = []
51
    ) {
52 18
        $data = $resource ? $this->fractal($request)->createData($resource)->toArray() : null;
53
54 17
        return RestResponse::create($data, $httStatus, $headers);
55
    }
56
57
    /**
58
     * @return RestResponse
59
     */
60 1
    public function noContent()
61
    {
62 1
        return RestResponse::noContent();
63
    }
64
}
65