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

BackpackCrudControllerService::getFieldsAsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
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 BackpackCrudControllerService extends ServiceAbstract implements ServiceInterface
10
{
11
    protected $relativeToBasePath = 'app/Http/Controllers/Admin';
12
13
    private $columns = [];
14
15
    private $fields = [];
16
17
    public function call()
18
    {
19
        $this->command->call('make:crud-controller', [
20
            'name' => $this->getName($this->command->entity),
21
        ]);
22
23
        $this->addLatestFileToIdeStack();
24
25
        $this->setColumns();
26
        $this->setFields();
27
28
        $this->insertColumnsAndFieldsInGeneratedController();
29
    }
30
31
    public function getName(string $entity): string
32
    {
33
        return ucfirst($entity);
34
    }
35
36
    private function setFields(): void
37
    {
38
        $this->command->schema->getStructure()->each(function ($fieldType) {
39
            array_push($this->fields, $fieldType->getField());
40
        });
41
    }
42
43
    private function setColumns(): void
44
    {
45
        $this->command->schema->getStructure()->each(function ($fieldType) {
46
            array_push($this->columns, $fieldType->getColumn());
47
        });
48
    }
49
50
    private function insertColumnsAndFieldsInGeneratedController(): void
51
    {
52
        $controllerFile = end($this->command->filesToBeOpened);
53
54
        $controller = $this->filesystem->get($controllerFile);
55
        $controller = str_replace('__columns__', $this->getColumnsAsString(), $controller);
56
        $controller = str_replace('__fields__', $this->getfieldsAsString(), $controller);
57
        $this->filesystem->put($controllerFile, $controller);
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    private function getColumnsAsString(): string
64
    {
65
        return ShortSyntaxArray::parse($this->columns);
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    private function getFieldsAsString(): string
72
    {
73
        return ShortSyntaxArray::parse($this->fields);
74
    }
75
}
76