Completed
Pull Request — master (#11)
by Rougin
02:34
created

Wildfire::setQueryAndTable()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 8
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
use Rougin\Wildfire\Helpers\ModelHelper;
6
7
/**
8
 * Wildfire
9
 *
10
 * Yet another wrapper for CodeIgniter's Query Builder Class.
11
 *
12
 * @package Wildfire
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class Wildfire extends \CI_Model
16
{
17
    use Traits\DatabaseTrait, Traits\ObjectTrait, Traits\ResultTrait;
18
19
    /**
20
     * @param \CI_DB_query_builder|null $database
21
     * @param \CI_DB_result|null        $query
22
     */
23 39
    public function __construct($database = null, $query = null)
24
    {
25 39
        if (! empty($database)) {
26 39
            $this->setDatabase($database);
27 39
        }
28
29 39
        $this->query = $query;
30 39
    }
31
32
    /**
33
     * Finds the row from the specified ID or with the list of delimiters from
34
     * the specified table.
35
     *
36
     * @param  string         $tableName
37
     * @param  array|integer  $delimiters
38
     * @return object|boolean
39
     */
40 24
    public function find($tableName, $delimiters = [])
41
    {
42 24
        if (is_integer($delimiters)) {
43 15
            $primaryKey = $this->describe->getPrimaryKey($tableName);
44
45 15
            $delimiters = [ $primaryKey => $delimiters ];
46 15
        }
47
48 21
        $this->db->where($delimiters);
49
50 21
        $query = $this->db->get($tableName);
51
52 21
        if ($query->num_rows() > 0 && $query->row()) {
53 15
            return $this->createObject($tableName, $query->row());
54
        }
55
56 6
        return false;
57
    }
58
59
    /**
60
     * Returns all rows from the specified table.
61
     *
62
     * @param  mixed $table
63
     * @return self
64
     */
65 24
    public function get($table = '')
66
    {
67
        // Guess the specified table from the query
68 24
        if (empty($table)) {
69 6
            $query = $this->db->last_query();
70
71 6
            preg_match('/\bfrom\b\s*(\w+)/i', $query, $matches);
72
73 6
            $this->table = $matches[1];
74
75 6
            return $this;
76
        }
77
78 18
        $this->setQueryAndTable($table);
79
80 18
        return $this;
81
    }
82
83
    /**
84
     * Sets the query and table properties.
85
     *
86
     * @param  string|object $table
87
     * @return void
88
     */
89 18
    protected function setQueryAndTable($table)
90
    {
91 18
        list($tableName, $model) = ModelHelper::createInstance($table);
92
93 18
        $this->table = $tableName;
94
95 18
        if ($this->query == null) {
96 18
            $this->query = $this->db->get($tableName);
97 18
        }
98
99 18
        if ($model == $table) {
100 6
            $this->table = $model;
101 6
        }
102
103 18
        return;
104
    }
105
106
    /**
107
     * Calls methods from this class in underscore case.
108
     *
109
     * @param  string $method
110
     * @param  mixed  $parameters
111
     * @return mixed
112
     */
113 9
    public function __call($method, $parameters)
114
    {
115 9
        $method = camelize($method);
116 9
        $result = $this;
117
118 9
        if (method_exists($this, $method)) {
119 9
            $class = [$this, $method];
120
            
121 9
            $result = call_user_func_array($class, $parameters);
122 9
        }
123
124 9
        return $result;
125
    }
126
}
127