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->messages() + $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.
@@ 210-258 (lines=49) @@
207
     * @param Request $request
208
     * @return JsonResponse
209
     */
210
    public function edit(Request $request)
211
    {
212
        $connection = $this->getBuilder()->getConnection();
213
        $affected   = [];
214
        $errors     = [];
215
216
        $connection->beginTransaction();
217
        foreach ($request->get('data') as $key => $data) {
218
            $model     = $this->getBuilder()->findOrFail($key);
219
            $validator = $this->getValidationFactory()
220
                              ->make($data, $this->editRules($model), $this->messages() + $this->editMessages(), $this->attributes());
221
            if ($validator->fails()) {
222
                foreach ($this->formatErrors($validator) as $error) {
223
                    $errors[] = $error;
224
                }
225
226
                continue;
227
            }
228
229
            if (method_exists($this, 'updating')) {
230
                $data = $this->updating($model, $data);
231
            }
232
233
            if (method_exists($this, 'saving')) {
234
                $data = $this->saving($model, $data);
235
            }
236
237
            $model->fill($data)->save();
238
239
            if (method_exists($this, 'updated')) {
240
                $model = $this->updated($model, $data);
241
            }
242
243
            if (method_exists($this, 'saved')) {
244
                $model = $this->saved($model, $data);
245
            }
246
247
            $model->setAttribute('DT_RowId', $model->getKey());
248
            $affected[] = $model;
249
        }
250
251
        if (! $errors) {
252
            $connection->commit();
253
        } else {
254
            $connection->rollBack();
255
        }
256
257
        return $this->toJson($affected, $errors);
258
    }
259
260
    /**
261
     * Get elqouent builder of the model.