Completed
Branch master (c6a504)
by Rougin
02:02
created

ResultTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 118
ccs 53
cts 53
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A asDropdown() 0 13 2
A result() 0 18 3
D createObject() 0 43 9
A getQueryResult() 0 10 2
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
/**
6
 * Result Trait
7
 *
8
 * Contains functions for retrieving data.
9
 * 
10
 * @package Wildfire
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
trait ResultTrait
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $tables = [];
19
20
    /**
21
     * Lists all data in dropdown format.
22
     *
23
     * @param  string $description
24
     * @return array
25
     */
26 3
    public function asDropdown($description = 'description')
27
    {
28 3
        $data = [];
29 3
        $id = $this->describe->getPrimaryKey($this->table);
0 ignored issues
show
Bug introduced by
The property describe does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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...
30
31 3
        $result = $this->query->result();
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
33 3
        foreach ($result as $row) {
34 3
            $data[$row->$id] = ucwords($row->$description);
35 3
        }
36
37 3
        return $data;
38
    }
39
40
    /**
41
     * Return the result
42
     * 
43
     * @return object
44
     */
45 9
    public function result()
46
    {
47 9
        $data = $this->getQueryResult();
48 9
        $result = [];
49
50 9
        if (empty($this->table)) {
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...
51 3
            $this->get();
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
52 3
        }
53
54 9
        foreach ($data as $row)
55
        {
56 9
            $object = $this->createObject($this->table, $row);
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...
57
58 9
            array_push($result, $object);
59 9
        }
60
61 9
        return $result;
62
    }
63
64
    /**
65
     * Create an object from the specified data
66
     *
67
     * @param  string $table
68
     * @param  object $row
69
     * @return array
70
     */
71 12
    protected function createObject($table, $row)
72
    {
73 12
        $newTable = ucfirst(Inflector::singular($table));
74 12
        $model = new $newTable;
75
76 12
        if ( ! array_key_exists($table, $this->tables)) {
77 12
            $tableInfo = $this->describe->getTable($table);
78
79 12
            $this->tables[$table] = $tableInfo;
80 12
        } else {
81 9
            $tableInfo = $this->tables[$table];
82
        }
83
84 12
        foreach ($row as $key => $value) {
85 12
            foreach ($tableInfo as $column) {
86 12
                if ($column->getField() != $key) {
87 12
                    continue;
88
                }
89
90 12
                $model->$key = $value;
91 12
            }
92 12
        }
93
94 12
        foreach ($row as $key => $value) {
95 12
            foreach ($tableInfo as $column) {
96 12
                if ($column->getField() != $key || ! $column->isForeignKey()) {
97 12
                    continue;
98
                }
99
100 12
                $foreignColumn = $column->getReferencedField();
101 12
                $foreignTable = $column->getReferencedTable();
102
103 12
                $delimiters = [ $foreignColumn => $value ];
104 12
                $foreignData = $this->find($foreignTable, $delimiters);
0 ignored issues
show
Bug introduced by
It seems like find() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
105
106 12
                $newColumn = Inflector::singular($foreignTable);
107
108 12
                $model->$newColumn = $foreignData;
109 12
            }
110 12
        }
111
112 12
        return $model;
113
    }
114
115
    /**
116
     * Gets the data result from the specified query.
117
     * 
118
     * @return array|object
119
     */
120 9
    protected function getQueryResult()
121
    {
122 9
        $result = $this->query;
123
124 9
        if (method_exists($this->query, 'result')) {
125 9
            $result = $this->query->result();
126 9
        }
127
128 9
        return $result;
129
    }
130
}