Passed
Push — master ( 1f7f34...948e88 )
by Thomas
04:12 queued 02:03
created

Schema::getFieldNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Schemas;
4
5
use Illuminate\Support\Collection;
6
use Webfactor\Laravel\Generators\Contracts\SchemaFieldTypeInterface;
7
use Webfactor\Laravel\Generators\Helper\RegexParser;
8
9
class Schema
10
{
11
    protected $structure = [];
12
13
    public function __construct(string $schema)
14
    {
15
        $this->parseSchemaFields($this->getSchemaFieldsFromSchemaString($schema));
16
    }
17
18
    /**
19
     * @return Collection
20
     */
21
    public function getStructure(): Collection
22
    {
23
        return collect($this->structure);
24
    }
25
26
    private function getSchemaFieldsFromSchemaString(string $schema): array
27
    {
28
        return explode(',', $schema);
29
    }
30
31
    private function parseSchemaFields(array $schemaFields): void
32
    {
33
        foreach ($schemaFields as $schemaField) {
34
            $this->parseSchemaField($schemaField);
35
        }
36
    }
37
38
    private function parseSchemaField(string $schemaField): void
39
    {
40
        $schemaFieldParts = explode(';', $schemaField);
41
42
        $this->setSchemaField($this->parseMigration(array_shift($schemaFieldParts)), $schemaFieldParts);
43
    }
44
45
    private function parseMigration(string $migrationString): array
46
    {
47
        ['left' => $left, 'inside' => $inside] = RegexParser::parseParenthesis($migrationString);
0 ignored issues
show
Bug introduced by
The variable $left does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $inside does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
48
        [$name, $type] = explode(':', $left);
0 ignored issues
show
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49
50
        return [
51
            'name' => $name,
52
            'type' => $type,
53
            'options' => $inside,
54
        ];
55
    }
56
57
    private function setSchemaField(array $migrationOptions, array $crudOptions): void
58
    {
59
        if ($schemaFieldType = $this->getSchemaFieldType($migrationOptions, $crudOptions)) {
60
            array_push($this->structure, $schemaFieldType);
61
        }
62
    }
63
64
    protected function getSchemaFieldType(array $migrationOptions, array $crudOptions): ?SchemaFieldTypeInterface
65
    {
66
        $typeClass = config('webfactor.generators.fieldTypes.'. $migrationOptions['type']);
67
68
        if (class_exists($typeClass)) {
69
            return $this->loadMigrationFieldType(new $typeClass($migrationOptions, $crudOptions));
70
        }
71
72
        return null;
73
    }
74
75
    private function loadMigrationFieldType(SchemaFieldTypeInterface $fieldType): SchemaFieldTypeInterface
76
    {
77
        return $fieldType;
78
    }
79
80
    public function getFieldNames()
81
    {
82
        return $this->getStructure()->pluck('name');
83
    }
84
85
    public function getMigrationFields()
86
    {
87
        return $this->getStructure()->pluck('migrationField');
88
    }
89
90
    public function getCrudFields()
91
    {
92
        return $this->getStructure()->pluck('crudField');
93
    }
94
95
    public function getCrudColumns()
96
    {
97
        return $this->getStructure()->pluck('crudColumn');
98
    }
99
100
    public function getValidationRules()
101
    {
102
        return $this->getStructure()->pluck('validationRule', 'name');
103
    }
104
}
105