Code Duplication    Length = 51-52 lines in 2 locations

src/DataTablesEditor.php 2 locations

@@ 59-109 (lines=51) @@
56
     * @param Request $request
57
     * @return JsonResponse
58
     */
59
    public function create(Request $request)
60
    {
61
        $instance   = $this->resolveModel();
62
        $connection = $instance->getConnection();
63
        $affected   = [];
64
        $errors     = [];
65
66
        $connection->beginTransaction();
67
        foreach ($request->get('data') as $data) {
68
            $validator = $this->getValidationFactory()
69
                              ->make($data, $this->createRules(), $this->createMessages(), $this->attributes());
70
            if ($validator->fails()) {
71
                foreach ($this->formatErrors($validator) as $error) {
72
                    $errors[] = $error;
73
                }
74
75
                continue;
76
            }
77
78
            $instance->fill($data);
79
80
            if (method_exists($this, 'creating')) {
81
                $data = $this->creating($instance, $data);
82
            }
83
84
            if (method_exists($this, 'saving')) {
85
                $data = $this->saving($instance, $data);
86
            }
87
88
            $instance->save();
89
90
            if (method_exists($this, 'created')) {
91
                $instance = $this->created($instance, $data);
92
            }
93
94
            if (method_exists($this, 'saved')) {
95
                $instance = $this->saved($instance, $data);
96
            }
97
98
            $instance->setAttribute('DT_RowId', $instance->getKey());
99
            $affected[] = $instance;
100
        }
101
102
        if (! $errors) {
103
            $connection->commit();
104
        } else {
105
            $connection->rollBack();
106
        }
107
108
        return $this->toJson($affected, $errors);
109
    }
110
111
    /**
112
     * Resolve model to used.
@@ 195-246 (lines=52) @@
192
     * @param Request $request
193
     * @return JsonResponse
194
     */
195
    public function edit(Request $request)
196
    {
197
        $connection = $this->getBuilder()->getConnection();
198
        $affected   = [];
199
        $errors     = [];
200
201
202
        $connection->beginTransaction();
203
        foreach ($request->get('data') as $key => $data) {
204
            $model     = $this->getBuilder()->findOrFail($key);
205
            $validator = $this->getValidationFactory()
206
                              ->make($data, $this->editRules($model), $this->editMessages(), $this->attributes());
207
            if ($validator->fails()) {
208
                foreach ($this->formatErrors($validator) as $error) {
209
                    $errors[] = $error;
210
                }
211
212
                continue;
213
            }
214
215
            $model->fill($data);
216
217
            if (method_exists($this, 'updating')) {
218
                $data = $this->updating($model, $data);
219
            }
220
221
            if (method_exists($this, 'saving')) {
222
                $data = $this->saving($model, $data);
223
            }
224
225
            $model->save();
226
227
            if (method_exists($this, 'updated')) {
228
                $model = $this->updated($model, $data);
229
            }
230
231
            if (method_exists($this, 'saved')) {
232
                $model = $this->saved($model, $data);
233
            }
234
235
            $model->setAttribute('DT_RowId', $model->getKey());
236
            $affected[] = $model;
237
        }
238
239
        if (! $errors) {
240
            $connection->commit();
241
        } else {
242
            $connection->rollBack();
243
        }
244
245
        return $this->toJson($affected, $errors);
246
    }
247
248
    /**
249
     * Get elqouent builder of the model.