Completed
Push — master ( f1f480...26274e )
by Arthur
17s queued 10s
created

Response::getRelated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SoliDry\Containers;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Http\Request;
7
use SoliDry\Extension\BaseFormRequest;
8
use SoliDry\Extension\JSONApiInterface;
9
use SoliDry\Helpers\Errors;
10
use SoliDry\Helpers\Json;
11
use SoliDry\Helpers\JsonApiResponse;
12
use SoliDry\Helpers\SqlOptions;
13
use League\Fractal\Resource\Collection as FractalCollection;
14
15
/**
16
 * Class Response
17
 *
18
 * @package SoliDry\Helpers
19
 *
20
 * @property SqlOptions sqlOptions
21
 */
22
class Response
23
{
24
    private $json;
25
    private $errors;
26
27
    private $formRequest;
28
    private $entity;
29
    private $sqlOptions;
30
    private $method;
31
32
    public function __construct(Json $json, Errors $errors)
33
    {
34
        $this->json = $json;
35
        $this->errors = $errors;
36
    }
37
38
    public function get($data, array $meta)
39
    {
40
        return $this->{'get' . ucfirst($this->method)}($data, $meta);
41
    }
42
43
    /**
44
     * @param Collection $data
45
     * @param array $meta
46
     * @return \Illuminate\Http\Response
47
     */
48
    public function getIndex($data, array $meta): \Illuminate\Http\Response
49
    {
50
        $resource = $this->json->setIsCollection(true)->setMeta($meta)
51
            ->getResource($this->formRequest, $data, $this->entity);
52
53
        return $this->getResponse(Json::prepareSerializedData($resource, $this->sqlOptions->getData()));
54
    }
55
56
    /**
57
     * @param $data
58
     * @param array $meta
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function getView($data, array $meta): \Illuminate\Http\Response
62
    {
63
        $resource = $this->json->setMeta($meta)->getResource($this->formRequest, $data, $this->entity);
64
65
        return $this->getResponse(Json::prepareSerializedData($resource,  $this->sqlOptions->getData()));
66
    }
67
68
    /**
69
     * @param $data
70
     * @param array $meta
71
     * @return \Illuminate\Http\Response
72
     */
73
    public function getCreate($data, array $meta): \Illuminate\Http\Response
74
    {
75
        $resource = $this->json->setMeta($meta)->getResource($this->formRequest, $data, $this->entity);
76
77
        return $this->getResponse(Json::prepareSerializedData($resource), JSONApiInterface::HTTP_RESPONSE_CODE_CREATED);
78
    }
79
80
    /**
81
     * @param $data
82
     * @param array $meta
83
     * @return \Illuminate\Http\Response
84
     */
85
    public function getUpdate($data, array $meta): \Illuminate\Http\Response
86
    {
87
        $resource = $this->json->setMeta($meta)->getResource($this->formRequest, $data, $this->entity);
88
89
        return $this->getResponse(Json::prepareSerializedData($resource));
90
    }
91
92
    /**
93
     * Gets an output for relations
94
     *
95
     * @param $relationModel
96
     * @param string $entity
97
     * @param Request $request
98
     * @return \Illuminate\Http\Response
99
     */
100
    public function getRelations($relationModel, string $entity, Request $request): \Illuminate\Http\Response
101
    {
102
        $resource = Json::getRelations($relationModel, $entity);
103
104
        return $this->getResponse(Json::prepareSerializedRelations($request, $resource));
105
    }
106
107
    /**
108
     * Gets an output for createRelations
109
     *
110
     * @param $data
111
     * @param array $meta
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function getCreateRelations($data, array $meta): \Illuminate\Http\Response
115
    {
116
        $resource = $this->json->setMeta($meta)->getResource($this->formRequest, $data, $this->entity);
117
118
        return $this->getResponse(Json::prepareSerializedData($resource), JSONApiInterface::HTTP_RESPONSE_CODE_CREATED);
119
    }
120
121
    /**
122
     * Gets an output for updateRelations
123
     *
124
     * @param $data
125
     * @param array $meta
126
     * @return \Illuminate\Http\Response
127
     */
128
    public function getUpdateRelations($data, array $meta): \Illuminate\Http\Response
129
    {
130
        $resource = $this->json->setMeta($meta)->getResource($this->formRequest, $data, $this->entity);
131
132
        return $this->getResponse(Json::prepareSerializedData($resource));
133
    }
134
135
    /**
136
     * Gets an output for deleteRelations
137
     *
138
     * @return \Illuminate\Http\Response
139
     */
140
    public function getDeleteRelations():  \Illuminate\Http\Response
141
    {
142
        return $this->getResponse(Json::prepareSerializedData(new FractalCollection()), JSONApiInterface::HTTP_RESPONSE_CODE_NO_CONTENT);
143
    }
144
145
146
    /**
147
     * Gets an output for createBulk
148
     *
149
     * @param $data
150
     * @param array $meta
151
     * @return \Illuminate\Http\Response
152
     */
153
    public function getCreateBulk($data, array $meta): \Illuminate\Http\Response
154
    {
155
        $resource = $this->json->setIsCollection(true)
156
            ->setMeta($meta)->getResource($this->formRequest, $data, $this->entity);
157
158
        return $this->getResponse(Json::prepareSerializedData($resource), JSONApiInterface::HTTP_RESPONSE_CODE_CREATED);
159
    }
160
161
    /**
162
     * Gets an output for updateBulk
163
     *
164
     * @param $data
165
     * @param array $meta
166
     * @return \Illuminate\Http\Response
167
     */
168
    public function getUpdateBulk($data, array $meta): \Illuminate\Http\Response
169
    {
170
        $resource = $this->json->setIsCollection(true)
171
            ->setMeta($meta)->getResource($this->formRequest, $data, $this->entity);
172
173
        return $this->getResponse(Json::prepareSerializedData($resource));
174
    }
175
176
    /**
177
     * Gets an output for removeBulk
178
     *
179
     * @return \Illuminate\Http\Response
180
     */
181
    public function removeBulk(): \Illuminate\Http\Response
182
    {
183
        return $this->getResponse(Json::prepareSerializedData(
184
            new FractalCollection()), JSONApiInterface::HTTP_RESPONSE_CODE_NO_CONTENT);
185
    }
186
187
    /**
188
     * Gets an output for related
189
     *
190
     * @param BaseFormRequest $relationData
191
     * @param $data
192
     * @return \Illuminate\Http\Response
193
     */
194
    public function getRelated($relationData, $data): \Illuminate\Http\Response
195
    {
196
        $this->json->setIsCollection($relationData instanceof  Collection);
197
        $resource = $this->json->getResource($this->formRequest, $relationData, $this->entity);
198
199
        return $this->getResponse(Json::prepareSerializedData($resource, $data));
200
    }
201
202
    /**
203
     * Gets an output for model not found error
204
     *
205
     * @param string $entity
206
     * @param $id
207
     * @return \Illuminate\Http\Response
208
     */
209
    public function getModelNotFoundError(string $entity, $id): \Illuminate\Http\Response
210
    {
211
        return $this->getResponse($this->json->getErrors($this->errors->getModelNotFound($entity, $id)),
212
            JSONApiInterface::HTTP_RESPONSE_CODE_NOT_FOUND);
213
    }
214
215
    /**
216
     * @param mixed $formRequest
217
     * @return Response
218
     */
219
    public function setFormRequest($formRequest): Response
220
    {
221
        $this->formRequest = $formRequest;
222
223
        return $this;
224
    }
225
226
    /**
227
     * @param mixed $entity
228
     * @return Response
229
     */
230
    public function setEntity($entity): Response
231
    {
232
        $this->entity = $entity;
233
234
        return $this;
235
    }
236
237
    /**
238
     * @param mixed $sqlOptions
239
     * @return Response
240
     */
241
    public function setSqlOptions(SqlOptions $sqlOptions): Response
242
    {
243
        $this->sqlOptions = $sqlOptions;
244
245
        return $this;
246
    }
247
248
    /**
249
     * Prepares Response object to return with particular http response, headers and body
250
     *
251
     * @param string $json
252
     * @param int $responseCode
253
     * @return \Illuminate\Http\Response
254
     */
255
    public function getResponse(string $json, int $responseCode = JSONApiInterface::HTTP_RESPONSE_CODE_OK) : \Illuminate\Http\Response
256
    {
257
        return (new JsonApiResponse())->getResponse($json, $responseCode);
258
    }
259
260
    /**
261
     * @param string $method
262
     */
263
    public function setMethod(string $method): void
264
    {
265
        $this->method = $method;
266
    }
267
}