Completed
Push — master ( 289621...5c183f )
by Rougin
02:34
created

DatabaseTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableName() 0 7 2
A setDatabase() 0 17 2
A setQuery() 0 6 1
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
use Rougin\SparkPlug\Instance;
6
7
/**
8
 * Database Trait
9
 * 
10
 * @package Wildfire
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
trait DatabaseTrait
14
{
15
    /**
16
     * @var \CI_DB
17
     */
18
    protected $db;
19
20
    /**
21
     * @var \CI_DB_result
22
     */
23
    protected $query;
24
25
    /**
26
     * Parses the table name from Describe class.
27
     * 
28
     * @param  string $table
29
     * @return string
30
     */
31 24
    protected function getTableName($table)
32
    {
33 24
        $table = ucfirst(singular($table));
34 24
        $array = explode('.', $table);
35
36 24
        return isset($array[1]) ? $array[1] : $table;
37
    }
38
39
    /**
40
     * Sets the database class.
41
     * 
42
     * @param  \CI_DB|null $database
43
     * @return self
44
     */
45 24
    public function setDatabase($database = null)
46
    {
47 24
        $ci = Instance::create();
48 24
        $ci->load->helper('inflector');
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...
49
50 24
        if (empty($database)) {
51 3
            $ci->load->database();
52
53 3
            $this->db = $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...
54
55 3
            return $this;
56
        }
57
58 24
        $this->db = $database;
59
60 24
        return $this;
61
    }
62
63
    /**
64
     * Sets the query result.
65
     * 
66
     * @param  \CI_DB_result $query
67
     * @return self
68
     */
69 3
    public function setQuery($query)
70
    {
71 3
        $this->query = $query;
72
73 3
        return $this;
74
    }
75
}
76