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

MigrationSchema::setMigrationField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Schemas;
4
5
use Illuminate\Support\Collection;
6
use Webfactor\Laravel\Generators\Contracts\FieldTypeInterface;
7
8
class MigrationSchema
9
{
10
    protected $structure;
11
12
    public function __construct(string $schema)
13
    {
14
        $this->structure = collect();
15
16
        foreach (explode(',', $schema) as $field) {
17
            $this->setMigrationField($field);
18
        }
19
    }
20
21
    private function setMigrationField($field)
22
    {
23
        $options = explode(':', $field);
24
25
        $name = array_shift($options);
26
        $type = array_shift($options);
27
28
        if ($fieldType = $this->getFieldType($type, $name, $options)) {
29
            $this->structure->push($fieldType);
30
        }
31
    }
32
33
    protected function getFieldType($type, $name, $options)
34
    {
35
        $typeClass = 'Webfactor\\Laravel\\Generators\\Schemas\\FieldTypes\\' . ucfirst($type) . 'Type';
36
37
        if (class_exists($typeClass)) {
38
            return $this->loadFieldType(new $typeClass($name, $options));
39
        }
40
41
        return null;
42
    }
43
44
    private function loadFieldType(FieldTypeInterface $fieldType)
45
    {
46
        return $fieldType;
47
    }
48
49
    /**
50
     * @return Collection
51
     */
52
    public function getStructure(): Collection
53
    {
54
        return $this->structure;
55
    }
56
}
57