Completed
Pull Request — master (#3)
by Rougin
02:51
created

ObjectTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 139
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createObject() 0 17 2
find() 0 1 ?
A getModel() 0 13 2
A getTableInformation() 0 12 2
A setForeignField() 0 19 3
B setModelValues() 0 17 6
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
use Rougin\Describe\Column;
6
7
use Rougin\Wildfire\CodeigniterModel;
8
use Rougin\Wildfire\Helpers\TableHelper;
9
10
/**
11
 * Object Trait
12
 *
13
 * @package Wildfire
14
 * @author  Rougin Royce Gutib <[email protected]>
15
 *
16
 * @property \Rougin\Describe\Describe $describe
17
 * @property string                    $table
18
 */
19
trait ObjectTrait
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $tables = [];
25
26
    /**
27
     * Creates an object from the specified table and row.
28
     *
29
     * @param  string|\Rougin\Wildfire\CodeigniterModel $table
30
     * @param  object                                   $row
31
     * @param  boolean                                  $isForeignKey
32
     * @return array
33
     */
34 30
    protected function createObject($table, $row, $isForeignKey = false)
35
    {
36 30
        list($tableName, $model) = $this->getModel($table, $isForeignKey);
37
38 30
        $properties = [];
39
40 30
        if ($model instanceof CodeigniterModel) {
41 30
            $properties = $model->getProperties();
42 30
            $properties = $model->getRelationshipProperties($properties);
43 30
        }
44
45 30
        $tableInfo = $this->getTableInformation($tableName);
46
47 30
        $this->setModelValues($model, $row, $properties, $tableInfo);
48
49 30
        return $model;
50
    }
51
52
    /**
53
     * Finds the row from the specified ID or with the list of delimiters from
54
     * the specified table.
55
     *
56
     * @param  string         $table
57
     * @param  array|integer  $delimiters
58
     * @param  boolean        $isForeignKey
59
     * @return object|boolean
60
     */
61
    abstract protected function find($table, $delimiters = [], $isForeignKey = false);
62
63
    /**
64
     * Gets the model class of the said table.
65
     *
66
     * @param  string|object $table
67
     * @param  boolean       $isForeignKey
68
     * @return array
69
     */
70 39
    protected function getModel($table, $isForeignKey = false)
71
    {
72 39
        $newModel = $table;
73
74 39
        if (! is_object($table)) {
75 36
            $modelName = TableHelper::getModelName($table, $this->table, $isForeignKey);
76 36
            $newModel  = new $modelName;
77 36
        }
78
79 39
        $newTable = TableHelper::getNameFromModel($newModel);
0 ignored issues
show
Bug introduced by
It seems like $newModel defined by $table on line 72 can also be of type string; however, Rougin\Wildfire\Helpers\...per::getNameFromModel() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
81 39
        return [ strtolower($newTable), $newModel ];
82
    }
83
84
    /**
85
     * Returns the database information of the specified table.
86
     *
87
     * @param  string $tableName
88
     * @return array
89
     */
90 30
    public function getTableInformation($tableName)
91
    {
92 30
        if (! array_key_exists($tableName, $this->tables)) {
93 24
            $tableInfo = $this->describe->getTable($tableName);
94
95 24
            $this->tables[$tableName] = $tableInfo;
96
97 24
            return $tableInfo;
98
        }
99
100 27
        return $this->tables[$tableName];
101
    }
102
103
    /**
104
     * Sets the foreign field of the column, if any.
105
     *
106
     * @param  \CI_Model               $model
107
     * @param  \Rougin\Describe\Column $column
108
     * @param  array                   $properties
109
     * @return void
110
     */
111 30
    protected function setForeignField(\CI_Model $model, Column $column, array $properties)
112
    {
113 30
        if (! $column->isForeignKey()) {
114 30
            return;
115
        }
116
117 18
        $columnName = $column->getField();
118
119 18
        $foreignColumn = $column->getReferencedField();
120 18
        $foreignTable  = $column->getReferencedTable();
121
122 18
        if (in_array($foreignTable, $properties['belongs_to'])) {
123 3
            $delimiters  = [ $foreignColumn => $model->$columnName ];
124 3
            $foreignData = $this->find($foreignTable, $delimiters, true);
125 3
            $newColumn   = TableHelper::getModelName($foreignTable, $this->table, true);
126
127 3
            $model->$newColumn = $foreignData;
128 3
        }
129 18
    }
130
131
    /**
132
     * Sets the model values based on the result row.
133
     *
134
     * @param  \CI_Model &$model
135
     * @param  object    $row
136
     * @param  array     $properties
137
     * @param  array     $tableInformation
138
     * @return void
139
     */
140 30
    protected function setModelValues(&$model, $row, $properties, $tableInformation)
141
    {
142 30
        foreach ($tableInformation as $column) {
143 30
            $key = $column->getField();
144
145 30
            $inColumns = ! empty($properties['columns']) && ! in_array($key, $properties['columns']);
146 30
            $inHiddenColumns = ! empty($properties['hidden']) && in_array($key, $properties['hidden']);
147
148 30
            if ($inColumns || $inHiddenColumns) {
149 30
                continue;
150
            }
151
152 30
            $model->$key = $row->$key;
153
154 30
            $this->setForeignField($model, $column, $properties);
155 30
        }
156 30
    }
157
}
158