Completed
Pull Request — master (#2)
by Rougin
02:50
created

DatabaseTrait::getClassTableName()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 6
nop 2
crap 5
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
 * @property string $table
14
 */
15
trait DatabaseTrait
16
{
17
    /**
18
     * @var \CI_DB
19
     */
20
    protected $db;
21
22
    /**
23
     * @var \CI_DB_result
24
     */
25
    protected $query;
26
27
    /**
28
     * Parses the table name from Describe class.
29
     *
30
     * @param  string  $table
31
     * @param  boolean $isForeignKey
32
     * @return string
33
     */
34 36
    protected function getClassTableName($table, $isForeignKey = false)
35
    {
36 36
        $tableName = $table;
37
38 36
        if (! $isForeignKey && $this->table) {
39 24
            $tableName = $this->table;
40
41 24
            if (is_object($this->table)) {
42 9
                $tableName = $this->table->getTableName();
43 9
            }
44 24
        }
45
46 36
        $tableName  = ucfirst(singular($tableName));
47 36
        $tableNames = explode('.', $tableName);
48
49 36
        return isset($tableNames[1]) ? $tableNames[1] : $tableName;
50
    }
51
52
    /**
53
     * Sets the database class.
54
     *
55
     * @param  \CI_DB|null $database
56
     * @return self
57
     */
58 39
    public function setDatabase($database = null)
59
    {
60 39
        $ci = Instance::create();
61
62 39
        $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...
63
64 39
        if (empty($database)) {
65 3
            $ci->load->database();
66
67 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...
68
69 3
            return $this;
70
        }
71
72 39
        $this->db = $database;
73
74 39
        return $this;
75
    }
76
77
    /**
78
     * Sets the query result.
79
     *
80
     * @param  \CI_DB_result $query
81
     * @return self
82
     */
83 3
    public function setQuery($query)
84
    {
85 3
        $this->query = $query;
86
87 3
        return $this;
88
    }
89
}
90