Code Duplication    Length = 49-49 lines in 2 locations

src/DataTablesEditor.php 2 locations

@@ 59-107 (lines=49) @@
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
            if (method_exists($this, 'creating')) {
79
                $data = $this->creating($instance, $data);
80
            }
81
82
            if (method_exists($this, 'saving')) {
83
                $data = $this->saving($instance, $data);
84
            }
85
86
            $instance->fill($data)->save();
87
88
            if (method_exists($this, 'created')) {
89
                $instance = $this->created($instance, $data);
90
            }
91
92
            if (method_exists($this, 'saved')) {
93
                $instance = $this->saved($instance, $data);
94
            }
95
96
            $instance->setAttribute('DT_RowId', $instance->getKey());
97
            $affected[] = $instance;
98
        }
99
100
        if (! $errors) {
101
            $connection->commit();
102
        } else {
103
            $connection->rollBack();
104
        }
105
106
        return $this->toJson($affected, $errors);
107
    }
108
109
    /**
110
     * Resolve model to used.
@@ 193-241 (lines=49) @@
190
     * @param Request $request
191
     * @return JsonResponse
192
     */
193
    public function edit(Request $request)
194
    {
195
        $connection = $this->getBuilder()->getConnection();
196
        $affected   = [];
197
        $errors     = [];
198
199
        $connection->beginTransaction();
200
        foreach ($request->get('data') as $key => $data) {
201
            $model     = $this->getBuilder()->findOrFail($key);
202
            $validator = $this->getValidationFactory()
203
                              ->make($data, $this->editRules($model), $this->editMessages(), $this->attributes());
204
            if ($validator->fails()) {
205
                foreach ($this->formatErrors($validator) as $error) {
206
                    $errors[] = $error;
207
                }
208
209
                continue;
210
            }
211
212
            if (method_exists($this, 'updating')) {
213
                $data = $this->updating($model, $data);
214
            }
215
216
            if (method_exists($this, 'saving')) {
217
                $data = $this->saving($model, $data);
218
            }
219
220
            $model->fill($data)->save();
221
222
            if (method_exists($this, 'updated')) {
223
                $model = $this->updated($model, $data);
224
            }
225
226
            if (method_exists($this, 'saved')) {
227
                $model = $this->saved($model, $data);
228
            }
229
230
            $model->setAttribute('DT_RowId', $model->getKey());
231
            $affected[] = $model;
232
        }
233
234
        if (! $errors) {
235
            $connection->commit();
236
        } else {
237
            $connection->rollBack();
238
        }
239
240
        return $this->toJson($affected, $errors);
241
    }
242
243
    /**
244
     * Get elqouent builder of the model.