Completed
Push — master ( 5247f7...5089a0 )
by Maarten
20s queued 16s
created

CreateFormRequestAction::toRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Actions\Resource;
4
5
use Mtolhuys\LaravelSchematics\Services\RuleParser;
6
use Mtolhuys\LaravelSchematics\Services\StubWriter;
7
8
class CreateFormRequestAction
9
{
10
    /**
11
     * @param $request
12
     * @return string
13
     */
14 View Code Duplication
    public function execute($request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
    {
16
        $model = $request['name'];
17
        $namespace = config('schematics.form-request-namespace');
18
        $stub = __DIR__ . '/../../../resources/stubs/form-request.stub';
19
        $file = app_path(str_replace(['App\\', '\\'], ['', '/'], $namespace) . "/Create{$model}Request.php");
20
21
        (new StubWriter($file, $stub))->write([
22
            '$namespace$' => $namespace,
23
            '$class$' => "Create{$model}Request",
24
            '$rules$' => trim($this->fieldsToRules($request['fields'])),
25
        ]);
26
    }
27
28
    /**
29
     * @param array $fields
30
     * @return string
31
     */
32
    private function fieldsToRules(array $fields): string
33
    {
34
        $rules = '';
35
36
        foreach ($fields as $index => $field) {
37
            $rule = RuleParser::parseRule($field['type']);
38
39
            if ($rule === null) {
40
                continue;
41
            }
42
43
            if ($index === 0) {
44
                $rules .= "'{$field['name']}' => '{$rule}',";
45
            } else {
46
                $rules .= PHP_EOL . str_repeat(' ', 12) . "'{$field['name']}' => '{$rule}',";
47
            }
48
        }
49
50
        return $rules;
51
    }
52
}
53