Completed
Push — master ( ad83ed...4c8d32 )
by James Ekow Abaka
03:09
created

ModelDescription   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 88.52%

Importance

Changes 7
Bugs 0 Features 4
Metric Value
wmc 21
c 7
b 0
f 4
lcom 1
cbo 4
dl 0
loc 115
ccs 54
cts 61
cp 0.8852
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelationships() 0 4 1
A getPrimaryKey() 0 4 1
B __construct() 0 32 5
A appendConstraints() 0 13 3
B getRelationshipDetails() 0 19 6
A createRelationships() 0 12 2
A getAutoPrimaryKey() 0 4 1
A getFields() 0 4 1
A getUniqueKeys() 0 4 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 8
    public function __construct($model)
18
    {
19 8
        $this->table = $model->getTable();
20 8
        $this->name = Nibii::getModelName((new \ReflectionClass($model))->getName());
21 8
        $relationships = $model->getRelationships();
22 8
        $adapter = DriverAdapter::getDefaultInstance();
23 8
        $schema = Db::getDriver()->describeTable($this->table)[$this->table];
24 8
        $this->autoPrimaryKey = $schema['auto_increment'];
25
26 8
        foreach ($schema['columns'] as $field => $details) {
27 8
            $this->fields[$field] = [
28 8
                'type' => $adapter->mapDataTypes($details['type']),
29 8
                'required' => !$details['nulls'],
30 8
                'default' => $details['default'],
31 8
                'name' => $field
32
            ];
33 8
            if(isset($details['default'])) {
34 4
                $this->fields[$field]['default'] = $details['default'];
35
            }
36 8
            if(isset($details['length'])) {
37 8
                $this->fields[$field]['length'] = $details['length'];
38
            }
39
        }
40
41 8
        $this->appendConstraints($schema['primary_key'], $this->primaryKey, true);
42 8
        $this->appendConstraints($schema['unique_keys'], $this->uniqueKeys);
43
44 8
        foreach($relationships as $type => $relations) {
45 8
            $this->createRelationships($type, $relations);
46
        }
47
48 8
    }
49
50 8
    private function appendConstraints($constraints, &$key, $flat = false)
51
    {
52 8
        foreach ($constraints as $constraint) {
53 8
            if ($flat) {
54 8
                $key = $constraint['columns'];
55 8
                break;
56
            } else {
57 4
                $key[] = array(
58 4
                    'fields' => $constraint['columns']
59
                );
60
            }
61
        }
62 8
    }
63
64 8
    private function getRelationshipDetails($relationship)
65
    {
66 8
        if(is_string($relationship))
67
        {
68
            return [
69 4
                'model' => $relationship,
70 4
                'name' => $relationship,
71 4
                'foreign_key' => '',
72 4
                'local_key' => ''
73
            ];
74 4
        } else if (is_array($relationship)) {
75
            return [
76 4
                'model' => $relationship[0],
77 4
                'name' => isset($relationship['as']) ? $relationship['as'] : $relationship[0],
78 4
                'foreign_key' => isset($relationship['foreign_key']) ? $relationship['foreign_key'] : '',
79 4
                'local_key' => isset($relationship['local_key']) ? $relationship['local_key'] : ''
80
            ];
81
        }
82
    }
83
84 8
    private function createRelationships($type, $relationships)
85
    {
86 8
        foreach($relationships as $relationship)
87
        {
88 8
            $relationship = $this->getRelationshipDetails($relationship);
89 8
            $class = "\\ntentan\\nibii\\relationships\\{$type}Relationship";
90 8
            $relationshipObject = new $class();
91 8
            $relationshipObject->setOptions($relationship);
92 8
            $relationshipObject->setup($this->name, $this->table, $this->primaryKey);
93 8
            $this->relationships[$relationship['name']] = $relationshipObject;
94
        }
95 8
    }
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
    public function getAutoPrimaryKey()
108
    {
109
        return $this->autoPrimaryKey;
110
    }
111
112
    public function getFields()
113
    {
114
        return $this->fields;
115
    }
116
    
117
    public function getUniqueKeys()
118
    {
119
        return $this->uniqueKeys;
120
    }
121
}
122