Completed
Push — master ( 4c8d32...c7efbc )
by James Ekow Abaka
04:08
created

ModelDescription::getUniqueKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ntentan\nibii;
4
5
use ntentan\atiaa\Db;
6
7
class ModelDescription
8
{
9
    private $fields = [];
10
    private $primaryKey = [];
11
    private $uniqueKeys = [];
12
    private $autoPrimaryKey = false;
13
    private $relationships = [];
14
    private $table;
15
    private $name;
16
17 28
    public function __construct($model)
18
    {
19 28
        $this->table = $model->getTable();
20 28
        $this->name = Nibii::getModelName((new \ReflectionClass($model))->getName());
21 28
        $relationships = $model->getRelationships();
22 28
        $adapter = DriverAdapter::getDefaultInstance();
23 28
        $schema = Db::getDriver()->describeTable($this->table)[$this->table];
24 28
        $this->autoPrimaryKey = $schema['auto_increment'];
25
26 28
        foreach ($schema['columns'] as $field => $details) {
27 28
            $this->fields[$field] = [
28 28
                'type' => $adapter->mapDataTypes($details['type']),
29 28
                'required' => !$details['nulls'],
30 28
                'default' => $details['default'],
31 28
                'name' => $field
32
            ];
33 28
            if(isset($details['default'])) {
34 20
                $this->fields[$field]['default'] = $details['default'];
35
            }
36 28
            if(isset($details['length'])) {
37 28
                $this->fields[$field]['length'] = $details['length'];
38
            }
39
        }
40
41 28
        $this->appendConstraints($schema['primary_key'], $this->primaryKey, true);
42 28
        $this->appendConstraints($schema['unique_keys'], $this->uniqueKeys);
43
44 28
        foreach($relationships as $type => $relations) {
45 28
            $this->createRelationships($type, $relations);
46
        }
47
48 28
    }
49
50 28
    private function appendConstraints($constraints, &$key, $flat = false)
51
    {
52 28
        foreach ($constraints as $constraint) {
53 28
            if ($flat) {
54 28
                $key = $constraint['columns'];
55 28
                break;
56
            } else {
57 16
                $key[] = array(
58 16
                    'fields' => $constraint['columns']
59
                );
60
            }
61
        }
62 28
    }
63
64 28
    private function getRelationshipDetails($relationship)
65
    {
66 28
        if(is_string($relationship))
67
        {
68
            return [
69 22
                'model' => $relationship,
70 22
                'name' => $relationship,
71 22
                'foreign_key' => '',
72 22
                'local_key' => ''
73
            ];
74 6
        } else if (is_array($relationship)) {
75
            return [
76 6
                'model' => $relationship[0],
77 6
                'name' => isset($relationship['as']) ? $relationship['as'] : $relationship[0],
78 6
                'foreign_key' => isset($relationship['foreign_key']) ? $relationship['foreign_key'] : '',
79 6
                'local_key' => isset($relationship['local_key']) ? $relationship['local_key'] : ''
80
            ];
81
        }
82
    }
83
84 28
    private function createRelationships($type, $relationships)
85
    {
86 28
        foreach($relationships as $relationship)
87
        {
88 28
            $relationship = $this->getRelationshipDetails($relationship);
89 28
            $class = "\\ntentan\\nibii\\relationships\\{$type}Relationship";
90 28
            $relationshipObject = new $class();
91 28
            $relationshipObject->setOptions($relationship);
92 28
            $relationshipObject->setup($this->name, $this->table, $this->primaryKey);
93 28
            $this->relationships[$relationship['name']] = $relationshipObject;
94
        }
95 28
    }
96
97 18
    public function getRelationships()
98
    {
99 18
        return $this->relationships;
100
    }
101
102 24
    public function getPrimaryKey()
103
    {
104 24
        return $this->primaryKey;
105
    }
106
107 10
    public function getAutoPrimaryKey()
108
    {
109 10
        return $this->autoPrimaryKey;
110
    }
111
112 10
    public function getFields()
113
    {
114 10
        return $this->fields;
115
    }
116
    
117 10
    public function getUniqueKeys()
118
    {
119 10
        return $this->uniqueKeys;
120
    }
121
}
122