Completed
Push — master ( 4c6c98...8c8647 )
by James Ekow Abaka
02:34
created

ModelDescription::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ntentan\nibii;
4
5
class ModelDescription {
6
7
    private $fields = [];
8
    private $primaryKey = [];
9
    private $uniqueKeys = [];
10
    private $autoPrimaryKey = false;
11
12
    /**
13
     *
14
     * @var array<Relationship>
15
     */
16
    private $relationships = [];
17
    private $table;
18
    private $schema;
19
    private $name;
20
    private $container;
21
22
    /**
23
     * 
24
     * @param RecordWrapper $model
25
     */
26 28
    public function __construct(ORMContext $context, $model) {
27 28
        $dbInformation = $model->getDBStoreInformation();
28 28
        $this->table = $dbInformation['table'];
29 28
        $this->schema = $dbInformation['schema'];
30 28
        $this->name = $context->getModelName((new \ReflectionClass($model))->getName());
31 28
        $this->container = $context->getContainer();
32 28
        $relationships = $model->getRelationships();
33 28
        $adapter = $context->getContainer()->resolve(DriverAdapter::class);
34 28
        $schema = array_values(
35 28
            $context->getDbContext()->getDriver()->describeTable(
36 28
                $dbInformation['unquoted_table']
37
            )
38 28
        )[0];
39 28
        $this->autoPrimaryKey = $schema['auto_increment'];
40
41 28
        foreach ($schema['columns'] as $field => $details) {
42 28
            $this->fields[$field] = [
43 28
                'type' => $adapter->mapDataTypes($details['type']),
44 28
                'required' => !$details['nulls'],
45 28
                'default' => $details['default'],
46 28
                'name' => $field
47
            ];
48 28
            if (isset($details['default'])) {
49 20
                $this->fields[$field]['default'] = $details['default'];
50
            }
51 28
            if (isset($details['length'])) {
52 28
                $this->fields[$field]['length'] = $details['length'];
53
            }
54
        }
55
56 28
        $this->appendConstraints($schema['primary_key'], $this->primaryKey, true);
57 28
        $this->appendConstraints($schema['unique_keys'], $this->uniqueKeys);
58
59 28
        foreach ($relationships as $type => $relations) {
60 28
            $this->createRelationships($type, $relations);
61
        }
62 28
    }
63
64 28
    private function appendConstraints($constraints, &$key, $flat = false) {
65 28
        foreach ($constraints as $constraint) {
66 28
            if ($flat) {
67 28
                $key = $constraint['columns'];
68 28
                break;
69
            } else {
70 16
                $key[] = array(
71 16
                    'fields' => $constraint['columns']
72
                );
73
            }
74
        }
75 28
    }
76
77 28
    private function getRelationshipDetails($relationship) {
78 28
        $relationshipDetails = [];
0 ignored issues
show
Unused Code introduced by
$relationshipDetails is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79 28
        if (is_string($relationship)) {
80
            $relationshipDetails = [
81 22
                'model' => $relationship,
82 22
                'name' => $relationship,
83 22
                'foreign_key' => '',
84 22
                'local_key' => ''
85
            ];
86 6
        } else if (is_array($relationship)) {
87
            $relationshipDetails = [
88 6
                'model' => $relationship[0],
89 6
                'name' => isset($relationship['as']) ? $relationship['as'] : $relationship[0],
90 6
                'foreign_key' => isset($relationship['foreign_key']) ? $relationship['foreign_key'] : '',
91 6
                'local_key' => isset($relationship['local_key']) ? $relationship['local_key'] : '',
92
            ];
93
        } else {
94
            return null;
95
        }
96 28
        $relationshipDetails['local_table'] = $this->table;
97 28
        if(isset($relationship['through'])) {
98
            $relationshipDetails['through'] = $relationship['through'];
99
        }
100 28
        return $relationshipDetails;
101
    }
102
103 28
    private function createRelationships($type, $relationships) {
104 28
        foreach ($relationships as $relationship) {
105 28
            $relationship = $this->getRelationshipDetails($relationship);
106 28
            $class = "\\ntentan\\nibii\\relationships\\{$type}Relationship";
107 28
            $relationshipObject = $this->container->resolve($class);
108 28
            $relationshipObject->setOptions($relationship);
109 28
            $relationshipObject->setup($this->name, $this->schema, $this->table, $this->primaryKey);
110 28
            $this->relationships[$relationship['name']] = $relationshipObject;
111
        }
112 28
    }
113
114
    /**
115
     * 
116
     * @return array<Relationship>
117
     */
118 18
    public function getRelationships() {
119 18
        return $this->relationships;
120
    }
121
122 24
    public function getPrimaryKey() {
123 24
        return $this->primaryKey;
124
    }
125
126 10
    public function getAutoPrimaryKey() {
127 10
        return $this->autoPrimaryKey;
128
    }
129
130 10
    public function getFields() {
131 10
        return $this->fields;
132
    }
133
134 10
    public function getUniqueKeys() {
135 10
        return $this->uniqueKeys;
136
    }
137
138
}
139