Passed
Push — master ( 4a2912...960495 )
by Iman
04:15
created

DbInspector   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
B findPK() 0 23 5
A isForeignKey() 0 17 3
A isNullableColumn() 0 3 1
A colName() 0 13 4
A getForeignKey() 0 9 2
A findPKname() 0 3 1
A listTables() 0 3 1
A getTableForeignKey() 0 4 2
1
<?php
2
3
namespace crocodicstudio\crudbooster\helpers;
4
5
use Cache;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, crocodicstudio\crudbooster\helpers\Cache. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use crocodicstudio\crudbooster\helpers\Cache as CbCache;
7
use DB;
8
use Illuminate\Support\Facades\Schema;
9
10
class DbInspector
11
{
12
    /**
13
     * @param $table
14
     * @return bool|null|string
15
     * @throws \Exception
16
     */
17
    public static function findPK($table)
18
    {
19
        if (! $table) {
20
            return 'id';
21
        }
22
23
        if (CbCache::get('table_'.$table, 'primaryKey')) {
24
            return CbCache::get('table_'.$table, 'primaryKey');
25
        }
26
        $table = CRUDBooster::parseSqlTable($table);
27
28
        if (! $table['table']) {
29
            throw new \Exception("parseSqlTable can't determine the table");
30
        }
31
32
        $primaryKey = self::findPKname($table);
33
34
        if (! $primaryKey) {
35
            return 'id';
36
        }
37
        CbCache::put('table_'.$table, 'primaryKey', $primaryKey);
38
39
        return $primaryKey;
40
    }
41
42
    /**
43
     * @param $table
44
     * @param $field
45
     * @return bool
46
     */
47
    public static function isNullableColumn($table, $field)
48
    {
49
        return !\DB::getDoctrineSchemaManager()->listTableColumns($table)[$field]->getNotnull();
50
    }
51
52
    /**
53
     * @param $columns
54
     * @return string
55
     */
56
    public static function colName($columns)
57
    {
58
        $nameColCandidate = explode(',', cbConfig('NAME_FIELDS_CANDIDATE'));
59
60
        foreach ($columns as $c) {
61
            foreach ($nameColCandidate as $cc) {
62
                if (strpos($c, $cc) !== false) {
63
                    return $c;
64
                }
65
            }
66
        }
67
68
        return 'id';
69
    }
70
71
    /**
72
     * @param $fieldName
73
     * @return bool
74
     */
75
    public static function isForeignKey($fieldName)
76
    {
77
        $cacheKey = 'isForeignKey_'.$fieldName;
78
79
        if (Cache::has($cacheKey)) {
80
            return Cache::get($cacheKey);
81
        }
82
83
        $table = self::getTableForeignKey($fieldName);
84
        if (! $table) {
85
            return false;
86
        }
87
88
        $hasTable = Schema::hasTable($table);
89
        Cache::forever($cacheKey, $hasTable);
90
91
        return $hasTable;
92
    }
93
94
    /**
95
     * @param $table
96
     * @return array
97
     */
98
    private static function findPKname($table)
99
    {
100
        return \DB::getDoctrineSchemaManager()->listTableDetails($table)->getPrimaryKey()->getColumns()[0];
101
    }
102
103
    public static function listTables()
104
    {
105
        return \DB::getDoctrineSchemaManager()->listTableNames();
106
    }
107
108
    public static function getForeignKey($parent_table, $child_table)
109
    {
110
        $parent_table = CRUDBooster::parseSqlTable($parent_table)['table'];
111
        $child_table = CRUDBooster::parseSqlTable($child_table)['table'];
112
113
        if (\Schema::hasColumn($child_table, 'id_'.$parent_table)) {
114
            return 'id_'.$parent_table;
115
        }
116
        return $parent_table.'_id';
117
    }
118
119
    public static function getTableForeignKey($fieldName)
120
    {
121
        if (self::isForeignKey($fieldName)) {
122
            return str_replace(['_id', 'id_'], '', $fieldName);
123
        }
124
    }
125
}