Completed
Pull Request — master (#8)
by Rougin
02:26
created

Wildfire::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 6
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 6
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
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 42
    public function __construct($database = null, $query = null)
24
    {
25 42
        if (! empty($database)) {
26 39
            $this->setDatabase($database);
27 39
        }
28
29 42
        $this->query = $query;
30 42
    }
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
        list($tableName, $model) = ModelHelper::createInstance($table);
79
80 18
        $this->table = $tableName;
81
82 18
        if ($this->query == null) {
83 18
            $this->query = $this->db->get($tableName);
84 18
        }
85
86 18
        if ($model == $table) {
87 6
            $this->table = $model;
88 6
        }
89
90 18
        return $this;
91
    }
92
93
    /**
94
     * Calls methods from this class in underscore case.
95
     *
96
     * @param  string $method
97
     * @param  mixed  $parameters
98
     * @return mixed
99
     */
100 9
    public function __call($method, $parameters)
101
    {
102 9
        $method = camelize($method);
103 9
        $result = $this;
104
105 9
        if (method_exists($this, $method)) {
106 9
            $class = [$this, $method];
107
            
108 9
            $result = call_user_func_array($class, $parameters);
109 9
        }
110
111 9
        return $result;
112
    }
113
114
    /**
115
     * Factory method to create Wildfire instance.
116
     *
117
     * @param \CI_DB|null        $database
118
     * @param \CI_DB_result|null $query
119
     */
120 18
    public static function create($database = null, $query = null)
121
    {
122 18
        return new Wildfire($database, $query);
123
    }
124
}
125