Passed
Push — master ( ba9ea6...f4d366 )
by Thomas
02:08 queued 11s
created

insertRulesInGeneratedRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
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
9
class BackpackCrudRequestService extends ServiceAbstract implements ServiceInterface
10
{
11
    protected $relativeToBasePath = 'app/Http/Requests/Admin';
12
13
    private $rules = [];
14
15
    public function call()
16
    {
17
        $this->command->call('make:crud-request', [
18
            'name' => $this->getName($this->command->entity),
19
        ]);
20
21
        $this->addLatestFileToIdeStack();
22
23
        $this->setRules();
24
25
        $this->insertRulesInGeneratedRequest();
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
    private function setRules(): void
38
    {
39
        $this->command->schema->getStructure()->each(function ($fieldType) {
40
            $this->rules[$fieldType->getName()] = $fieldType->getRule();
41
        });
42
    }
43
44 View Code Duplication
    private function insertRulesInGeneratedRequest(): 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...
45
    {
46
        $requestFile = end($this->command->filesToBeOpened);
47
48
        $request = $this->filesystem->get($requestFile);
49
        $request = str_replace('__rules__', $this->getRulesAsString(), $request);
50
        $this->filesystem->put($requestFile, $request);
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    private function getRulesAsString(): string
57
    {
58
        return ShortSyntaxArray::parse($this->rules);
59
    }
60
}
61