Completed
Pull Request — master (#8)
by Rougin
05:18 queued 01:36
created

ObjectTrait::createObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
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\ModelHelper;
9
use Rougin\Wildfire\Helpers\TableHelper;
10
11
/**
12
 * Object Trait
13
 *
14
 * @package Wildfire
15
 * @author  Rougin Royce Gutib <[email protected]>
16
 *
17
 * @property \Rougin\Describe\Describe $describe
18
 * @property string                    $table
19
 */
20
trait ObjectTrait
21
{
22
    /**
23
     * Creates an object from the specified table and row.
24
     *
25
     * @param  string|\Rougin\Wildfire\CodeigniterModel $table
26
     * @param  object                                   $row
27
     * @return array
28
     */
29 33
    protected function createObject($table, $row)
30
    {
31 33
        list($tableName, $model) = ModelHelper::createInstance($table);
32
33 33
        $properties = [];
34
35 33
        if ($model instanceof CodeigniterModel) {
36 33
            $properties = $model->getProperties();
37 33
            $properties = $model->getRelationshipProperties($properties);
38 33
        }
39
40 33
        $tableInfo = $this->describe->getTable($tableName);
41
42 33
        $this->setModelFields($model, $row, $properties, $tableInfo);
43
44 30
        return $model;
45
    }
46
47
    /**
48
     * Finds the row from the specified ID or with the list of delimiters from
49
     * the specified table.
50
     *
51
     * @param  string         $table
52
     * @param  array|integer  $delimiters
53
     * @return object|boolean
54
     */
55
    abstract protected function find($table, $delimiters = []);
56
57
    /**
58
     * Sets the foreign field of the column, if any.
59
     *
60
     * @param  \CI_Model               $model
61
     * @param  \Rougin\Describe\Column $column
62
     * @param  array                   $properties
63
     * @return void
64
     */
65 33
    protected function setForeignField(\CI_Model $model, Column $column, array $properties)
66
    {
67 33
        if (! $column->isForeignKey()) {
68 33
            return;
69
        }
70
71 15
        $columnName = $column->getField();
72
73 15
        $foreignColumn = $column->getReferencedField();
74 15
        $foreignTable  = $column->getReferencedTable();
75
76 15
        if (in_array($foreignTable, $properties['belongs_to'])) {
77
            $delimiters = [ $foreignColumn => $model->$columnName ];
78
            $foreign    = $this->find($foreignTable, $delimiters);
79
            $tableName  = TableHelper::getNameFromModel($foreign);
0 ignored issues
show
Bug introduced by
It seems like $foreign defined by $this->find($foreignTable, $delimiters) on line 78 can also be of type boolean; 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
            $model->$tableName = $foreign;
82
        }
83 15
    }
84
85
    /**
86
     * Sets the model values based on the result row.
87
     *
88
     * @param  \CI_Model &$model
89
     * @param  object    $row
90
     * @param  array     $properties
91
     * @param  array     $tableInformation
92
     * @return void
93
     */
94 33
    protected function setModelFields(&$model, $row, $properties, $tableInformation)
95
    {
96 33
        foreach ($tableInformation as $column) {
97 33
            $key = $column->getField();
98
99 33
            $inColumns = ! empty($properties['columns']) && ! in_array($key, $properties['columns']);
100 33
            $inHiddenColumns = ! empty($properties['hidden']) && in_array($key, $properties['hidden']);
101
102 33
            if ($inColumns || $inHiddenColumns) {
103 33
                continue;
104
            }
105
106 33
            $model->$key = $row->$key;
107
108 33
            $this->setForeignField($model, $column, $properties);
109 33
        }
110 30
    }
111
}
112