WildfireTrait::wildfire()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
use Rougin\Wildfire\Wildfire;
6
7
/**
8
 * @property \CI_DB_result $db
9
 *
10
 * @method string table()
11
 *
12
 * @package Wildfire
13
 *
14
 * @author Rougin Gutib <[email protected]>
15
 */
16
trait WildfireTrait
17
{
18
    /**
19
     * @var \Rougin\Wildfire\Wildfire
20
     */
21
    protected $wildfire;
22
23
    /**
24
     * Calls a method from the Wildfire instance.
25
     *
26
     * @param string  $method
27
     * @param mixed[] $params
28
     *
29
     * @return self
30 3
     * @throws \BadMethodCallException
31
     */
32 3
    public function __call($method, $params)
33
    {
34 3
        /** @var callable */
35
        $class = array($this->wildfire, $method);
36 3
37
        /** @var \Rougin\Wildfire\Wildfire */
38
        $wildfire = call_user_func_array($class, $params);
39
40
        $this->wildfire = $wildfire;
41
42
        return $this;
43
    }
44
45
    /**
46 3
     * Finds the row from storage based on given identifier.
47
     *
48 3
     * @param integer $id
49
     *
50
     * @return \Rougin\Wildfire\Model|null
51
     */
52
    public function find($id)
53
    {
54
        return $this->init()->find($this->table(), $id);
55
    }
56
57
    /**
58
     * Returns an array of rows from a specified table.
59 6
     *
60
     * @param integer|null $limit
61 6
     * @param integer|null $offset
62
     *
63
     * @return \Rougin\Wildfire\Wildfire
64
     */
65
    public function get($limit = null, $offset = null)
66
    {
67
        return $this->init()->get($this->table(), $limit, $offset);
68
    }
69
70 9
    /**
71
     * Sets the Wildfire instance.
72 9
     *
73
     * @param \Rougin\Wildfire\Wildfire $wildfire
74 9
     *
75
     * @return self
76
     */
77
    public function wildfire(Wildfire $wildfire)
78
    {
79
        $this->init($wildfire);
80
81
        return $this;
82
    }
83
84
    /**
85
     * Initializes the Wildfire instance.
86
     *
87
     * @param \Rougin\Wildfire\Wildfire|null $wildfire
88
     *
89
     * @return \Rougin\Wildfire\Wildfire
90
     */
91
    private function init(Wildfire $wildfire = null)
92
    {
93
        if ($wildfire)
94
        {
95
            $this->wildfire = $wildfire;
96
        }
97
        else
98
        {
99
            $this->wildfire = new Wildfire($this->db);
100
        }
101
102
        return $this->wildfire;
103
    }
104
}
105