Passed
Push — master ( 3cb518...ba9ea6 )
by Thomas
40s
created

BackpackCrudRequestService::getRulesAsString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Services;
4
5
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract;
6
use Webfactor\Laravel\Generators\Contracts\ServiceInterface;
7
use Webfactor\Laravel\Generators\Helper\ShortSyntaxArray;
8
use Webfactor\Laravel\Generators\Schemas\ValidationRule;
9
10
class BackpackCrudRequestService extends ServiceAbstract implements ServiceInterface
11
{
12
    protected $relativeToBasePath = 'app/Http/Requests/Admin';
13
14
    private $rules = [];
15
16
    public function call()
17
    {
18
        $this->command->call('make:crud-request', [
19
            'name' => $this->getName($this->command->entity),
20
        ]);
21
22
        $this->addLatestFileToIdeStack();
23
24
        $this->setRulesFromSchema();
25
        $this->fillRulesInGeneratedRequest();
26
    }
27
28
    /**
29
     * @param string $entity
30
     * @return string
31
     */
32
    public function getName(string $entity): string
33
    {
34
        return ucfirst($entity);
35
    }
36
37 View Code Duplication
    private function fillRulesInGeneratedRequest(): void
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...
38
    {
39
        $requestFile = end($this->command->filesToBeOpened);
40
41
        $request = $this->filesystem->get($requestFile);
42
        $request = str_replace('__rules__', $this->getRulesAsString(), $request);
43
        $this->filesystem->put($requestFile, $request);
44
    }
45
46
    private function setRulesFromSchema(): void
47
    {
48
        $this->command->schema->getStructure()->each(function ($field) {
49
            array_push($this->rules, new ValidationRule($field));
50
        });
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    private function getRulesAsString(): string
57
    {
58
        $rulesArray = [];
59
60
        foreach ($this->rules as $validationRule) {
61
            if ($ruleString = $validationRule->generateRuleString()) {
62
                $rulesArray[$validationRule->getField()] = $ruleString;
63
            }
64
        }
65
66
        return ShortSyntaxArray::parse($rulesArray);
67
    }
68
}
69