Completed
Push — master ( 5e8e24...d52d7e )
by Alexandr
01:40
created

AdminMethodsStore   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 3 Features 1
Metric Value
wmc 9
c 4
b 3
f 1
lcom 1
cbo 7
dl 0
loc 58
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D store() 0 39 9
1
<?php
2
3
namespace Larrock\Core\Traits;
4
5
use Session;
6
use Redirect;
7
use Validator;
8
use Larrock\Core\Component;
9
use Illuminate\Http\Request;
10
use Larrock\Core\Helpers\MessageLarrock;
11
use Larrock\Core\Events\ComponentItemStored;
12
use Larrock\Core\Helpers\FormBuilder\FormDate;
13
14
trait AdminMethodsStore
15
{
16
    /** @var Component */
17
    protected $config;
18
19
    /**
20
     * @var bool Разрешать ли делать редиректы
21
     * Возвращать пользователя на страницу вместо ответа api
22
     */
23
    public $allow_redirect = true;
24
25
    /**
26
     * Store a newly created resource in storage.
27
     *
28
     * @param  \Illuminate\Http\Request $request
29
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
30
     * @throws \Exception
31
     */
32
    public function store(Request $request)
33
    {
34
        $data = $this->config->getModel();
35
        $data->fill($request->all());
36
37
        foreach ($this->config->rows as $row) {
38
            if (\in_array($row->name, $data->getFillable(), false) && ! isset($data->{$row->name})) {
39
                if ($row instanceof FormDate) {
40
                    $data->{$row->name} = $request->input('date', date('Y-m-d'));
41
                } else {
42
                    $data->{$row->name} = $request->input($row->name, $row->default);
43
                }
44
            }
45
        }
46
47
        $validator = Validator::make($data->toArray(), $this->config->getValid());
48
        if ($validator->fails()) {
49
            if ($this->allow_redirect) {
50
                if (array_key_exists('url', $validator->failed())) {
51
                    $search = $this->config->getModel()::whereUrl($data->url)->first();
52
                    MessageLarrock::danger('Материал с тарим url уже существует: /admin/'.$this->config->name.'/'.$search->id.'/edit');
53
                }
54
55
                return back()->withInput($request->except('password'))->withErrors($validator);
56
            }
57
58
            return response()->json(['status' => 'danger', 'message' => $validator->errors()->first()]);
59
        }
60
61
        $data->save();
62
        event(new ComponentItemStored($this->config, $data, $request));
63
        \Cache::flush();
64
        Session::push('message.success', 'Материал '.$request->input('title').' добавлен');
65
        if ($this->allow_redirect) {
66
            return Redirect::to('/admin/'.$this->config->name.'/'.$data->id.'/edit')->withInput();
67
        }
68
69
        return $data;
70
    }
71
}
72