Completed
Push — master ( 533b47...6c16e6 )
by Gabriel
02:01
created

Cache::getCacheId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nip\Database\Metadata;
4
5
use Nip\Cache\Manager as CacheManager;
6
use Nip\Database\Connections\Connection;
7
8
/**
9
 * Class Cache
10
 * @package Nip\Database\Metadata
11
 */
12
class Cache extends CacheManager
13
{
14
    protected $metadata;
15
16
    public function __construct()
17
    {
18
        $this->setTtl(10 * 24 * 60 * 60);
0 ignored issues
show
Bug introduced by
The method setTtl() does not seem to exist on object<Nip\Database\Metadata\Cache>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
19
        $this->setActive(true);
20
    }
21
22
    /**
23
     * @param $table
24
     * @return mixed
25
     */
26
    public function describeTable($table)
27
    {
28
        $cacheId = $this->getCacheId($table);
29
30
        return $this->get($cacheId);
31
    }
32
33
    /**
34
     * @param $table
35
     * @return string
36
     */
37
    public function getCacheId($table)
38
    {
39
        return $this->getConnection()->getDatabase() . '.' . $table;
40
    }
41
42
    /**
43
     * @return Connection
44
     */
45
    public function getConnection()
46
    {
47
        return $this->getMetadata()->getConnection();
48
    }
49
50
    /**
51
     * @return Manager
52
     */
53
    public function getMetadata()
54
    {
55
        return $this->metadata;
56
    }
57
58
    /**
59
     * @param $metadata
60
     * @return $this
61
     */
62
    public function setMetadata($metadata)
63
    {
64
        $this->metadata = $metadata;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param $cacheId
71
     * @return mixed
72
     */
73
    public function get($cacheId)
74
    {
75
        if (!$this->valid($cacheId)) {
76
            $this->reload($cacheId);
77
        }
78
79
        return $this->getData($cacheId);
80
    }
81
82
    /**
83
     * @param $cacheId
84
     * @return mixed
85
     */
86
    public function reload($cacheId)
87
    {
88
        return $this->saveData($cacheId, $this->generate($cacheId));
89
    }
90
91
    /**
92
     * @param $cacheId
93
     * @return mixed
94
     */
95
    public function generate($cacheId)
96
    {
97
        $data = $this->getConnection()->describeTable($cacheId);
98
        $this->data[$cacheId] = $data;
99
100
        return $data;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function cachePath()
107
    {
108
        return parent::cachePath() . '/db-metadata/';
109
    }
110
}
111