Completed
Pull Request — master (#1)
by Rougin
03:18
created

ObjectTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.84%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 130
ccs 45
cts 49
cp 0.9184
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
D createObject() 0 44 10
find() 0 1 ?
A setForeignField() 0 16 2
B getModel() 0 18 5
getTableName() 0 1 ?
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
use CI_Model;
6
use Rougin\Describe\Column;
7
8
/**
9
 * Object Trait
10
 *
11
 * @package Wildfire
12
 * @author  Rougin Royce Gutib <[email protected]>
13
 *
14
 * @property \Rougin\Describe\Describe $describe
15
 */
16
trait ObjectTrait
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $tables = [];
22
23
    /**
24
     * Creates an object from the specified table and row.
25
     *
26
     * @param  string  $table
27
     * @param  object  $row
28
     * @param  boolean $isForeignKey
29
     * @return array
30
     */
31 27
    protected function createObject($table, $row, $isForeignKey = false)
32
    {
33 27
        list($model, $newTable) = $this->getModel($table, $isForeignKey);
34
35 27
        if (! array_key_exists($newTable, $this->tables)) {
36 21
            $tableInfo = $this->describe->getTable($newTable);
37
38 21
            $this->tables[$newTable] = $tableInfo;
39 21
        } else {
40 24
            $tableInfo = $this->tables[$newTable];
41
        }
42
43 27
        $columns = [];
44 27
        $hidden  = [];
45
46 27
        if (method_exists($model, 'getColumns')) {
47 12
            $columns = $model->getColumns();
48 27
        } elseif (property_exists($model, 'columns')) {
49
            // NOTE: To be removed in v1.0.0
50
            $columns = $model->columns;
51
        }
52
53
        // NOTE: To be removed in v1.0.0 (if condition only)
54 27
        if (method_exists($model, 'getHiddenColumns')) {
55 12
            $hidden = $model->getHiddenColumns();
56 12
        }
57
58 27
        foreach ($tableInfo as $column) {
59 27
            $key = $column->getField();
60
61 27
            $inHiddenColumns = ! empty($hidden) && in_array($key, $hidden);
62 27
            $inColumns       = ! empty($columns) && ! in_array($key, $columns);
63
64 27
            if ($inColumns || $inHiddenColumns) {
65 12
                continue;
66
            }
67
68 27
            $model->$key = $row->$key;
69
70 27
            $this->setForeignField($model, $column);
71 27
        }
72
73 27
        return $model;
74
    }
75
76
    /**
77
     * Finds the row from the specified ID or with the list of delimiters from
78
     * the specified table.
79
     *
80
     * @param  string         $table
81
     * @param  array|integer  $delimiters
82
     * @param  boolean        $isForeignKey
83
     * @return object|boolean
84
     */
85
    abstract protected function find($table, $delimiters = [], $isForeignKey = false);
86
87
    /**
88
     * Sets the foreign field of the column, if any.
89
     *
90
     * @param  \CI_Model               $model
91
     * @param  \Rougin\Describe\Column $column
92
     * @return void
93
     */
94 27
    protected function setForeignField(CI_Model $model, Column $column)
95
    {
96 27
        if (! $column->isForeignKey()) {
97 27
            return;
98
        }
99
100 15
        $columnName    = $column->getField();
101 15
        $foreignColumn = $column->getReferencedField();
102 15
        $foreignTable  = $column->getReferencedTable();
103
104 15
        $delimiters  = [ $foreignColumn => $model->$columnName ];
105 15
        $foreignData = $this->find($foreignTable, $delimiters, true);
106 15
        $newColumn   = $this->getTableName($foreignTable, true);
107
108 15
        $model->$newColumn = $foreignData;
109 15
    }
110
111
    /**
112
     * Gets the model class of the said table.
113
     *
114
     * @param  string|null $table
115
     * @param  boolean     $isForeignKey
116
     * @return array
117
     */
118 36
    protected function getModel($table = null, $isForeignKey = false)
119
    {
120 36
        if ($table == null && $this->table == null) {
0 ignored issues
show
Bug introduced by
The property table does not seem to exist. Did you mean tables?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
It seems like you are loosely comparing $table of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
121 6
            return [ null, '' ];
122
        }
123
124 36
        $newTable = $this->getTableName($table, $isForeignKey);
125 36
        $model = new $newTable;
126
127 36
        if (method_exists($model, 'getTableName')) {
128 15
            $newTable = $model->getTableName();
129 36
        } elseif (property_exists($model, 'table')) {
130
            // NOTE: To be removed in v1.0.0
131
            $newTable = $model->table;
132
        }
133
134 36
        return [ $model, strtolower($newTable) ];
135
    }
136
137
    /**
138
     * Parses the table name from Describe class.
139
     *
140
     * @param  string  $table
141
     * @param  boolean $isForeignKey
142
     * @return string
143
     */
144
    abstract protected function getTableName($table, $isForeignKey = false);
145
}
146