Completed
Push — master ( 496ba9...4a5f56 )
by Alexandr
04:02
created

AdminMethodsCreate::create()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
dl 0
loc 31
c 1
b 1
f 1
rs 6.7272
cc 7
eloc 19
nc 5
nop 1
1
<?php
2
3
namespace Larrock\Core\Traits;
4
5
use Larrock\Core\Component;
6
use Illuminate\Http\Request;
7
use Larrock\Core\Helpers\MessageLarrock;
8
use Larrock\Core\Helpers\FormBuilder\FormCategory;
9
10
trait AdminMethodsCreate
11
{
12
    /** @var Component */
13
    protected $config;
14
15
    /**
16
     * Creating a new resource.
17
     * @param Request $request
18
     * @return \Illuminate\Http\Response
19
     * @throws \Exception
20
     */
21
    public function create(Request $request)
22
    {
23
        $post_rows = [
24
            'title' => $request->get('title', 'Новый материал'),
25
            'url' => str_slug($request->get('title', 'Новый материал')),
26
        ];
27
28
        if ($request->has('category')) {
29
            $post_rows['category'] = $request->get('category');
30
        } else {
31
            foreach ($this->config->rows as $row) {
32
                if ($row->fillable && $row instanceof FormCategory) {
33
                    if ($findCategory = \LarrockCategory::getModel()->whereComponent($this->config->name)->first()) {
34
                        $post_rows[$row->name] = $findCategory->id;
35
                    } else {
36
                        MessageLarrock::danger('Создать материал пока нельзя. Сначала создайте для него раздел');
37
38
                        return back()->withInput();
39
                    }
40
                }
41
            }
42
        }
43
44
        $test = Request::create('/admin/'.$this->config->name, 'POST', $post_rows);
45
46
        if (! method_exists($this, 'store')) {
47
            $trait = new class { use AdminMethodsStore; };
48
            return $trait->store($test);
49
        }
50
        return $this->store($test);
0 ignored issues
show
Bug introduced by
It seems like store() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
    }
52
}
53