Completed
Pull Request — master (#3)
by Rougin
02:51
created

TableHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 50
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getModelName() 0 17 5
A getNameFromModel() 0 14 3
1
<?php
2
3
namespace Rougin\Wildfire\Helpers;
4
5
/**
6
 * Table Helper
7
 *
8
 * @package Wildfire
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 */
11
class TableHelper
12
{
13
    /**
14
     * Parses the table name from Describe class.
15
     *
16
     * @param  string  $table
17
     * @param  mixed   $currentTable
18
     * @param  boolean $isForeignKey
19
     * @return string
20
     */
21 36
    public static function getModelName($table, $currentTable, $isForeignKey = false)
22
    {
23 36
        $tableName = $table;
24
25 36
        if (! $isForeignKey && $currentTable) {
26 24
            $tableName = $currentTable;
27
28 24
            if (is_object($currentTable)) {
29 9
                $tableName = $currentTable->getTableName();
30 9
            }
31 24
        }
32
33 36
        $tableName  = ucfirst(singular($tableName));
34 36
        $tableNames = explode('.', $tableName);
35
36 36
        return isset($tableNames[1]) ? $tableNames[1] : $tableName;
37
    }
38
39
    /**
40
     * Gets the table name specified from the model.
41
     * NOTE: To be removed in v1.0.0. Use $model->getTableName()
42
     *
43
     * @param  object $model
44
     * @return string
45
     */
46 39
    public static function getNameFromModel($model)
47
    {
48 39
        $tableName = '';
49
50 39
        if (method_exists($model, 'getTableName')) {
51 39
            $tableName = $model->getTableName();
52 39
        }
53
54 39
        if (isset($model->table)) {
55 3
            $tableName = $model->table;
56 3
        }
57
58 39
        return $tableName;
59
    }
60
}
61