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

Wildfire::getQueryResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
use Rougin\Describe\Describe;
6
use Rougin\SparkPlug\Instance;
7
use Rougin\Describe\Driver\CodeIgniterDriver;
8
9
/**
10
 * Wildfire
11
 *
12
 * Yet another wrapper for CodeIgniter's Query Builder Class.
13
 * 
14
 * @package Wildfire
15
 * @author  Rougin Royce Gutib <[email protected]>
16
 */
17
class Wildfire
18
{
19
    use ResultTrait;
20
21
    /**
22
     * @var CI_DB
23
     */
24
    protected $db;
25
26
    /**
27
     * @var \Rougin\Describe\Describe
28
     */
29
    protected $describe;
30
31
    /**
32
     * @var CI_DB
33
     */
34
    protected $query;
35
36
    /**
37
     * @var string
38
     */
39
    protected $table = '';
40
41
    /**
42
     * @param CI_DB|null        $database
43
     * @param CI_DB_result|null $query
44
     */
45 18
    public function __construct($database = null, $query = null)
46
    {
47 18
        $config = [];
48
49 18
        if (empty($database)) {
50 3
            $ci = Instance::create();
51
52 3
            $ci->load->database();
0 ignored issues
show
Bug introduced by
The property load does not seem to exist in CI_Controller.

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...
53
54 3
            $database = $ci->db;
0 ignored issues
show
Bug introduced by
The property db does not seem to exist in CI_Controller.

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...
55 3
        }
56
57 18
        $this->db = $database;
58 18
        $this->query = $query;
0 ignored issues
show
Documentation Bug introduced by
It seems like $query can also be of type object<Rougin\Wildfire\CI_DB_result>. However, the property $query is declared as type object<Rougin\Wildfire\CI_DB>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
60 18
        $config['default'] = [
61 18
            'dbdriver' => $database->dbdriver,
62 18
            'hostname' => $database->hostname,
63 18
            'username' => $database->username,
64 18
            'password' => $database->password,
65 18
            'database' => $database->database
66 18
        ];
67
68 18
        if (empty($config['default']['hostname'])) {
69 18
            $config['default']['hostname'] = $database->dsn;
70 18
        }
71
72 18
        $driver = new CodeIgniterDriver($config);
73 18
        $this->describe = new Describe($driver);
74 18
    }
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
     * @return object|boolean
83
     */
84 15
    public function find($table, $delimiters = [])
85
    {
86 15
        if ( ! is_array($delimiters)) {
87 6
            $primaryKey = $this->describe->getPrimaryKey($table);
88
89 6
            $delimiters = [ $primaryKey => $delimiters ];
90 6
        }
91
92 15
        $this->db->where($delimiters);
93
94 15
        $query = $this->db->get($table);
95
96 15
        if ($query->num_rows() > 0) {
97 12
            return $this->createObject($table, $query->row());
98
        }
99
100 3
        return false;
101
    }
102
103
    /**
104
     * Return all rows from the specified table.
105
     * 
106
     * @param  string $table
107
     * @return self
108
     */
109 12
    public function get($table = '')
110
    {
111 12
        if ($this->query == null) {
112 9
            $this->query = $this->db->get($table);
113 9
        }
114
115 12
        $this->table = $table;
116
117
        // Guess the specified table from the query
118 12
        if (empty($table)) {
119 3
            $query = $this->db->last_query();
120
121 3
            preg_match('/\bfrom\b\s*(\w+)/i', $query, $matches);
122
123 3
            $this->table = $matches[1];
124 3
        }
125
126 12
        return $this;
127
    }
128
129
    /**
130
     * Sets the database class.
131
     * 
132
     * @param  CI_DB $database
133
     * @return self
134
     */
135 3
    public function setDatabase($database)
136
    {
137 3
        $this->db = $database;
138
139 3
        return $this;
140
    }
141
142
    /**
143
     * Calls methods from this class in underscore case.
144
     * 
145
     * @param  string $method
146
     * @param  mixed  $parameters
147
     * @return mixed
148
     */
149 6
    public function __call($method, $parameters) {
150 6
        $method = Inflector::camelize($method);
151 6
        $result = $this;
152
153 6
        if (method_exists($this, $method)) {
154 6
            $class = [$this, $method];
155
            
156 6
            $result = call_user_func_array($class, $parameters);
157 6
        }
158
159 6
        return $result;
160
    }
161
}
162