Response::getDeleteRelations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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