AdminMethodsCreate.php$0 ➔ create()   B
last analyzed

Complexity

Conditions 9

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
c 0
b 0
f 0
cc 9
rs 7.6604
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
use Larrock\ComponentCategory\Models\Category;
0 ignored issues
show
Bug introduced by
The type Larrock\ComponentCategory\Models\Category was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
trait AdminMethodsCreate
12
{
13
    /** @var Component */
14
    protected $config;
15
16
    /**
17
     * Creating a new resource.
18
     * @param Request $request
19
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
20
     * @throws \Exception
21
     */
22
    public function create(Request $request)
23
    {
24
        $post_rows = [
25
            'title' => $request->get('title', 'Новый материал'),
26
            'url' => str_slug($request->get('title', 'Новый материал')),
27
        ];
28
29
        if ($request->has('category')) {
30
            $post_rows['category'] = $request->get('category');
31
        }
32
33
        foreach ($this->config->rows as $row) {
34
            if (isset($row->modelChild) && $row->modelChild === \config('larrock.models.category', Category::class)) {
35
                if (! empty($request->get($row->name))) {
36
                    if ($findCategory = \LarrockCategory::getModel()->whereComponent($this->config->name)->whereId($request->get($row->name))->first()) {
0 ignored issues
show
Bug introduced by
The type LarrockCategory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
                        $post_rows[$row->name] = $findCategory->id;
38
                    } else {
39
                        MessageLarrock::danger('Раздела с переданным id:'.$request->get($row->name).' не существует');
40
41
                        return back()->withInput();
42
                    }
43
                } else {
44
                    if ($findCategory = \LarrockCategory::getModel()->whereComponent($this->config->name)->first()) {
45
                        $post_rows[$row->name] = $findCategory->id;
46
                    } else {
47
                        MessageLarrock::danger('Создать материал пока нельзя. Сначала создайте для него раздел');
48
49
                        return back()->withInput();
50
                    }
51
                }
52
            }
53
        }
54
55
        $store = Request::create('/admin/'.$this->config->name, 'POST', $post_rows);
56
57
        if (! method_exists($this, 'store')) {
58
            $trait = new class {
59
                use AdminMethodsStore;
0 ignored issues
show
introduced by
The trait Larrock\Core\Traits\AdminMethodsStore requires some properties which are not provided by anonymous//src/Traits/AdminMethodsCreate.php$0: $name, $url, $id, $default, $fillable, $rows
Loading history...
60
            };
61
62
            return $trait->updateConfig($this->config)->store($store);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $trait->updateCon...>config)->store($store) also could return the type Illuminate\Database\Eloquent\Model which is incompatible with the documented return type Illuminate\Http\Redirect...lluminate\Http\Response.
Loading history...
63
        }
64
65
        return $this->store($store);
66
    }
67
}
68