Schema   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 101
ccs 0
cts 26
cp 0
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A indexedColumns() 0 11 1
A indexes() 0 4 1
A lifetime() 0 3 1
A getManager() 0 3 1
A foreignKeys() 0 4 1
A columns() 0 12 1
1
<?php
2
3
namespace Terranet\Administrator;
4
5
use Cache;
6
use Carbon\Carbon;
7
use DateTime;
8
use Doctrine\DBAL\Schema\MySqlSchemaManager;
9
10
class Schema
11
{
12
    /**
13
     * @var MySqlSchemaManager
14
     */
15
    private $manager;
16
17
    public function __construct($manager)
18
    {
19
        $this->manager = $manager;
20
    }
21
22
    /**
23
     * Get list of indexed columns.
24
     *
25
     * @param $table
26
     *
27
     * @return array
28
     */
29
    public function indexedColumns($table)
30
    {
31
        return Cache::remember("{$table}_indexed_columns", $this->lifetime(), function () use ($table) {
32
            $indexedColumns = array_reduce($this->indexes($table), function ($indexedColumns, $index) {
33
                return array_merge($indexedColumns, $index->getColumns());
34
            }, []);
35
36
            $indexedColumns = array_unique($indexedColumns);
37
            sort($indexedColumns);
38
39
            return $indexedColumns;
40
        });
41
    }
42
43
    /**
44
     * List table indexes.
45
     *
46
     * @param $table
47
     *
48
     * @return \Doctrine\DBAL\Schema\Index[]
49
     */
50
    public function indexes($table)
51
    {
52
        return Cache::remember("{$table}_indexes", $this->lifetime(), function () use ($table) {
53
            return $this->manager->listTableIndexes($table);
54
        });
55
    }
56
57
    /**
58
     * List table columns.
59
     *
60
     * @param $table
61
     *
62
     * @return \Doctrine\DBAL\Schema\Column[]
63
     */
64
    public function columns($table)
65
    {
66
        return Cache::remember("{$table}_columns", $this->lifetime(), function () use ($table) {
67
            $columns = $this->manager->listTableColumns($table);
68
            $keys = array_keys($columns);
69
            $vals = array_values($columns);
70
71
            $keys = array_map(function ($key) {
72
                return trim(str_replace(['`', '"'], '', $key));
73
            }, $keys);
74
75
            return array_combine($keys, $vals);
76
        });
77
    }
78
79
    /**
80
     * list table foreign keys.
81
     *
82
     * @param $table
83
     *
84
     * @return mixed
85
     */
86
    public function foreignKeys($table)
87
    {
88
        return Cache::remember("{$table}_foreign_keys", $this->lifetime(), function () use ($table) {
89
            return $this->manager->listTableForeignKeys($table);
90
        });
91
    }
92
93
    /**
94
     * @return MySqlSchemaManager
95
     */
96
    public function getManager()
97
    {
98
        return $this->manager;
99
    }
100
101
    /**
102
     * Cache lifetime.
103
     *
104
     * @param  int  $seconds
105
     *
106
     * @return DateTime
107
     */
108
    protected function lifetime($seconds = 300)
109
    {
110
        return Carbon::now()->addSeconds($seconds);
111
    }
112
}
113