Schema   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 97
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getStructure() 0 4 1
A getSchemaFieldsFromSchemaString() 0 4 1
A parseSchemaFields() 0 6 2
A parseSchemaField() 0 6 1
A parseMigration() 0 11 1
A setSchemaField() 0 6 2
A getSchemaFieldType() 0 10 2
A loadMigrationFieldType() 0 4 1
A getFieldNames() 0 4 1
A getMigrationFields() 0 4 1
A getCrudFields() 0 4 1
A getCrudColumns() 0 4 1
A getValidationRules() 0 4 1
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
        $schema = str_replace(', ', ',', $schema);
16
        $this->parseSchemaFields($this->getSchemaFieldsFromSchemaString($schema));
17
    }
18
19
    /**
20
     * @return Collection
21
     */
22
    public function getStructure(): Collection
23
    {
24
        return collect($this->structure);
25
    }
26
27
    private function getSchemaFieldsFromSchemaString(string $schema): array
28
    {
29
        return explode(',', $schema);
30
    }
31
32
    private function parseSchemaFields(array $schemaFields): void
33
    {
34
        foreach ($schemaFields as $schemaField) {
35
            $this->parseSchemaField($schemaField);
36
        }
37
    }
38
39
    private function parseSchemaField(string $schemaField): void
40
    {
41
        $schemaFieldParts = explode(';', $schemaField);
42
43
        $this->setSchemaField($this->parseMigration(array_shift($schemaFieldParts)), $schemaFieldParts);
44
    }
45
46
    private function parseMigration(string $migrationString): array
47
    {
48
        ['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...
49
        [$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...
50
51
        return [
52
            'name' => $name,
53
            'type' => $type,
54
            'options' => $inside,
55
        ];
56
    }
57
58
    private function setSchemaField(array $migrationOptions, array $crudOptions): void
59
    {
60
        if ($schemaFieldType = $this->getSchemaFieldType($migrationOptions, $crudOptions)) {
61
            array_push($this->structure, $schemaFieldType);
62
        }
63
    }
64
65
    protected function getSchemaFieldType(array $migrationOptions, array $crudOptions): ?SchemaFieldTypeInterface
66
    {
67
        $typeClass = config('webfactor.generators.fieldTypes.'. $migrationOptions['type']);
68
69
        if (class_exists($typeClass)) {
70
            return $this->loadMigrationFieldType(new $typeClass($migrationOptions, $crudOptions));
71
        }
72
73
        return null;
74
    }
75
76
    private function loadMigrationFieldType(SchemaFieldTypeInterface $fieldType): SchemaFieldTypeInterface
77
    {
78
        return $fieldType;
79
    }
80
81
    public function getFieldNames()
82
    {
83
        return $this->getStructure()->pluck('name');
84
    }
85
86
    public function getMigrationFields()
87
    {
88
        return $this->getStructure()->pluck('migrationField');
89
    }
90
91
    public function getCrudFields()
92
    {
93
        return $this->getStructure()->pluck('crudField');
94
    }
95
96
    public function getCrudColumns()
97
    {
98
        return $this->getStructure()->pluck('crudColumn');
99
    }
100
101
    public function getValidationRules()
102
    {
103
        return $this->getStructure()->pluck('validationRule', 'name');
104
    }
105
}
106