Completed
Push — master ( 4cf390...186a7f )
by Gabriel
02:55
created

Cache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 12.9%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 103
ccs 4
cts 31
cp 0.129
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A describeTable() 0 6 1
A getCacheId() 0 4 1
A getConnection() 0 4 1
A getMetadata() 0 4 1
A setMetadata() 0 6 1
A get() 0 8 2
A reload() 0 8 2
A generate() 0 7 1
A cachePath() 0 4 1
1
<?php
2
3
namespace Nip\Database\Metadata;
4
5
use Nip\Cache\Manager as CacheManager;
6
use Nip\Database\Connections\Connection;
7
use Nip\Database\Connections\HasConnectionTrait;
8
9
/**
10
 * Class Cache
11
 * @package Nip\Database\Metadata
12
 */
13
class Cache extends CacheManager
14
{
15
    protected $metadata;
16
17 1
    public function __construct()
18
    {
19 1
        $this->setTtl(10 * 24 * 60 * 60);
20 1
        $this->setActive(true);
21 1
    }
22
23
    /**
24
     * @param $table
25
     * @return mixed
26
     */
27
    public function describeTable($table)
28
    {
29
        $cacheId = $this->getCacheId($table);
30
31
        return $this->get($cacheId);
32
    }
33
34
    /**
35
     * @param $table
36
     * @return string
37
     */
38
    public function getCacheId($table)
39
    {
40
        return $this->getConnection()->getDatabase() . '.' . $table;
41
    }
42
43
    /**
44
     * @return Connection
45
     */
46
    public function getConnection()
47
    {
48
        return $this->getMetadata()->getConnection();
49
    }
50
51
    /**
52
     * @return Manager
53
     */
54
    public function getMetadata()
55
    {
56
        return $this->metadata;
57
    }
58
59
    /**
60
     * @param $metadata
61
     * @return $this
62
     */
63
    public function setMetadata($metadata)
64
    {
65
        $this->metadata = $metadata;
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param $cacheId
72
     * @return mixed
73
     */
74
    public function get($cacheId)
75
    {
76
        if (!$this->valid($cacheId)) {
77
            $this->reload($cacheId);
78
        }
79
80
        return $this->getData($cacheId);
81
    }
82
83
    /**
84
     * @param $cacheId
85
     * @return mixed
86
     */
87
    public function reload($cacheId)
88
    {
89
        $data = $this->generate($cacheId);
90
        if (isset($data['fields'])) {
91
            return $this->saveData($cacheId, $data);
92
        }
93
        return false;
94
    }
95
96
    /**
97
     * @param $cacheId
98
     * @return mixed
99
     */
100
    public function generate($cacheId)
101
    {
102
        $data = $this->getConnection()->describeTable($cacheId);
103
        $this->data[$cacheId] = $data;
104
105
        return $data;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function cachePath()
112
    {
113
        return parent::cachePath() . '/db-metadata/';
114
    }
115
}
116