Code Duplication    Length = 49-49 lines in 2 locations

src/DataTablesEditor.php 2 locations

@@ 75-123 (lines=49) @@
72
     * @param Request $request
73
     * @return JsonResponse
74
     */
75
    public function create(Request $request)
76
    {
77
        $instance   = $this->resolveModel();
78
        $connection = $instance->getConnection();
79
        $affected   = [];
80
        $errors     = [];
81
82
        $connection->beginTransaction();
83
        foreach ($request->get('data') as $data) {
84
            $validator = $this->getValidationFactory()
85
                              ->make($data, $this->createRules(), $this->createMessages(), $this->attributes());
86
            if ($validator->fails()) {
87
                foreach ($this->formatErrors($validator) as $error) {
88
                    $errors[] = $error;
89
                }
90
91
                continue;
92
            }
93
94
            if (method_exists($this, 'creating')) {
95
                $data = $this->creating($instance, $data);
96
            }
97
98
            if (method_exists($this, 'saving')) {
99
                $data = $this->saving($instance, $data);
100
            }
101
102
            $instance->fill($data)->save();
103
104
            if (method_exists($this, 'created')) {
105
                $instance = $this->created($instance, $data);
106
            }
107
108
            if (method_exists($this, 'saved')) {
109
                $instance = $this->saved($instance, $data);
110
            }
111
112
            $instance->setAttribute('DT_RowId', $instance->getKey());
113
            $affected[] = $instance;
114
        }
115
116
        if (! $errors) {
117
            $connection->commit();
118
        } else {
119
            $connection->rollBack();
120
        }
121
122
        return $this->toJson($affected, $errors);
123
    }
124
125
    /**
126
     * Resolve model to used.
@@ 209-257 (lines=49) @@
206
     * @param Request $request
207
     * @return JsonResponse
208
     */
209
    public function edit(Request $request)
210
    {
211
        $connection = $this->getBuilder()->getConnection();
212
        $affected   = [];
213
        $errors     = [];
214
215
        $connection->beginTransaction();
216
        foreach ($request->get('data') as $key => $data) {
217
            $model     = $this->getBuilder()->findOrFail($key);
218
            $validator = $this->getValidationFactory()
219
                              ->make($data, $this->editRules($model), $this->editMessages(), $this->attributes());
220
            if ($validator->fails()) {
221
                foreach ($this->formatErrors($validator) as $error) {
222
                    $errors[] = $error;
223
                }
224
225
                continue;
226
            }
227
228
            if (method_exists($this, 'updating')) {
229
                $data = $this->updating($model, $data);
230
            }
231
232
            if (method_exists($this, 'saving')) {
233
                $data = $this->saving($model, $data);
234
            }
235
236
            $model->fill($data)->save();
237
238
            if (method_exists($this, 'updated')) {
239
                $model = $this->updated($model, $data);
240
            }
241
242
            if (method_exists($this, 'saved')) {
243
                $model = $this->saved($model, $data);
244
            }
245
246
            $model->setAttribute('DT_RowId', $model->getKey());
247
            $affected[] = $model;
248
        }
249
250
        if (! $errors) {
251
            $connection->commit();
252
        } else {
253
            $connection->rollBack();
254
        }
255
256
        return $this->toJson($affected, $errors);
257
    }
258
259
    /**
260
     * Get elqouent builder of the model.