Properties::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 18
ccs 14
cts 14
cp 1
rs 9.8333
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Cerbero\EloquentInspector\Components;
4
5
use Cerbero\EloquentInspector\Dtos\Property;
6
use Cerbero\EloquentInspector\Exceptions\UnknownType;
7
use Illuminate\Support\Facades\Schema;
8
9
/**
10
 * The properties component.
11
 *
12
 */
13
class Properties extends Component
14
{
15
    /**
16
     * The map between schema and PHP types.
17
     *
18
     * @var array
19
     */
20
    protected $typesMap = [
21
        'guid'     => 'string',
22
        'boolean'  => 'bool',
23
        'datetime' => 'Carbon\Carbon',
24
        'string'   => 'string',
25
        'json'     => 'string',
26
        'integer'  => 'int',
27
        'date'     => 'Carbon\Carbon',
28
        'smallint' => 'int',
29
        'text'     => 'string',
30
        'decimal'  => 'float',
31
        'bigint'   => 'int',
32
    ];
33
34
    /**
35
     * Retrieve the model properties
36
     *
37
     * @return array
38
     */
39 2
    public function get(): array
40
    {
41 2
        $properties = [];
42 2
        $model = $this->model::make();
43 2
        $table = $model->getTable();
44 2
        $connection = $model->getConnection();
45
46 2
        foreach (Schema::getColumnListing($table) as $name) {
47 2
            $column = $connection->getDoctrineColumn($table, $name);
48 2
            $properties[$name] = $property = new Property();
49 2
            $property->name = $name;
50 2
            $property->dbType = Schema::getColumnType($table, $name);
51 2
            $property->nullable = !$column->getNotnull();
52 2
            $property->default = $column->getDefault();
53 2
            $property->type = $this->typesMap[$property->dbType] ?? throw new UnknownType($this->model, $property);
54
        }
55
56 1
        return $properties;
57
    }
58
}
59