Passed
Branch master (f24b8d)
by Pavel
01:56
created

FractalResponse::transformer()   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\Response;
2
3
use Doctrine\ORM\QueryBuilder;
4
use Doctrine\ORM\Tools\Pagination\Paginator;
5
use League\Fractal\Manager;
6
use League\Fractal\Pagination\DoctrinePaginatorAdapter;
7
use League\Fractal\Resource\Collection;
8
use League\Fractal\Resource\Item;
9
use League\Fractal\Serializer\JsonApiSerializer;
10
use League\Fractal\TransformerAbstract;
11
12
use Pz\Doctrine\Rest\Request\CreateRequestInterface;
13
use Pz\Doctrine\Rest\Request\DeleteRequestInterface;
14
use Pz\Doctrine\Rest\Request\IndexRequestInterface;
15
use Pz\Doctrine\Rest\Request\ShowRequestInterface;
16
use Pz\Doctrine\Rest\Request\UpdateRequestInterface;
17
use Pz\Doctrine\Rest\RestException;
18
use Pz\Doctrine\Rest\RestRequestInterface;
19
use Pz\Doctrine\Rest\RestResponse;
20
use Pz\Doctrine\Rest\RestResponseFactory;
21
22
23
class FractalResponse implements RestResponseFactory
24
{
25
    const JSON_API_CONTENT_TYPE = 'application/vnd.api+json';
26
27
    /**
28
     * @var TransformerAbstract
29
     */
30
    protected $transformer;
31
32
    /**
33
     * FractalResponse constructor.
34
     *
35
     * @param TransformerAbstract $transformer
36
     */
37 15
    public function __construct(TransformerAbstract $transformer)
38
    {
39 15
        $this->transformer = $transformer;
40 15
    }
41
42
    /**
43
     * @param IndexRequestInterface $request
44
     * @param QueryBuilder          $qb
45
     *
46
     * @return RestResponse
47
     */
48 2
    public function index(IndexRequestInterface $request, QueryBuilder $qb)
49
    {
50 2
        $resource = new Collection($paginator = new Paginator($qb), $this->transformer());
51 2
        $resource->setPaginator(new DoctrinePaginatorAdapter($paginator, $this->getPaginatorRouteGenerator()));
52
53 2
        return $this->response(
54 2
            $this->fractal($request)
55 2
                ->createData($resource)
56 2
                ->toArray()
57
        );
58
    }
59
60
    /**
61
     * @param ShowRequestInterface $request
62
     * @param             $entity
63
     *
64
     * @return array
65
     */
66 1
    public function show(ShowRequestInterface $request, $entity)
67
    {
68 1
        return $this->response(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($...sformer()))->toArray()) returns the type Pz\Doctrine\Rest\RestResponse which is incompatible with the documented return type array.
Loading history...
69 1
            $this->fractal($request)
70 1
                ->createData(new Item($entity, $this->transformer()))
71 1
                ->toArray()
72
        );
73
    }
74
75
    /**
76
     * @param CreateRequestInterface $request
77
     * @param               $entity
78
     *
79
     * @return array
80
     */
81 1
    public function create(CreateRequestInterface $request, $entity)
82
    {
83 1
        return $this->response(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($...sformer()))->toArray()) returns the type Pz\Doctrine\Rest\RestResponse which is incompatible with the documented return type array.
Loading history...
84 1
            $this->fractal($request)
85 1
                ->createData(new Item($entity, $this->transformer()))
86 1
                ->toArray()
87
        );
88
    }
89
90
    /**
91
     * @param UpdateRequestInterface $request
92
     * @param               $entity
93
     *
94
     * @return array
95
     */
96 1
    public function update(UpdateRequestInterface $request, $entity)
97
    {
98 1
        return $this->response(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($...sformer()))->toArray()) returns the type Pz\Doctrine\Rest\RestResponse which is incompatible with the documented return type array.
Loading history...
99 1
            $this->fractal($request)
100 1
                ->createData(new Item($entity, $this->transformer()))
101 1
                ->toArray()
102
        );
103
    }
104
105
    /**
106
     * @param DeleteRequestInterface $request
107
     * @param               $entity
108
     *
109
     * @return RestResponse
110
     */
111 1
    public function delete(DeleteRequestInterface $request, $entity)
112
    {
113 1
        return $this->response();
114
    }
115
116
    /**
117
     * @param RestRequestInterface $request
118
     *
119
     * @return RestResponse
120
     */
121 3
    public function notFound(RestRequestInterface $request)
122
    {
123 3
        return $this->response(null, RestResponse::HTTP_NOT_FOUND);
124
    }
125
126
    /**
127
     * @param \Error|\Exception|\Pz\Doctrine\Rest\RestException $exception
128
     *
129
     * @return RestResponse
130
     * @throws \Error|\Exception|RestException
131
     */
132 6
    public function exception($exception)
133
    {
134 6
        $message = $exception->getMessage();
135
136
        switch (true) {
137 6
            case ($exception instanceof RestException):
138 5
                $httpStatus = $exception->httpStatus();
139 5
                $errors = $exception->errors();
140 5
                break;
141
142
            default:
143 1
                throw $exception;
144
                // $httpStatus = RestResponse::HTTP_INTERNAL_SERVER_ERROR;
145
                // $errors = $exception->getTrace();
146
                break;
147
        }
148
149 5
        return $this->response(['message' => $message, 'errors' => $errors], $httpStatus);
150
    }
151
152
    /**
153
     * Return configured fractal by request format.
154
     *
155
     * @param RestRequestInterface $request
156
     *
157
     * @return Manager
158
     */
159 5
    protected function fractal(RestRequestInterface $request)
160
    {
161 5
        $fractal = new Manager();
162
163 5
        if (in_array(static::JSON_API_CONTENT_TYPE, $request->http()->getAcceptableContentTypes())) {
164 1
            $fractal->setSerializer(new JsonApiSerializer());
165
        }
166
167 5
        if ($includes = $request->http()->get('include')) {
168 1
            $fractal->parseIncludes($includes);
169
        }
170
171 5
        if ($excludes = $request->http()->get('exclude')) {
172 1
            $fractal->parseExcludes($excludes);
173
        }
174
175 5
        return $fractal;
176
    }
177
178
    /**
179
     * @return TransformerAbstract
180
     */
181 5
    protected function transformer()
182
    {
183 5
        return $this->transformer;
184
    }
185
186
    /**
187
     * @param null $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
188
     * @param int  $httStatus
189
     *
190
     * @return RestResponse
191
     */
192 14
    protected function response($data = null, $httStatus = RestResponse::HTTP_OK)
193
    {
194 14
        return new RestResponse($data, $httStatus);
195
    }
196
197
    /**
198
     * @return \Closure
199
     */
200
    protected function getPaginatorRouteGenerator()
201
    {
202 2
        return function() {
203 2
            return null;
204 2
        };
205
    }
206
}
207