Completed
Push — master ( c26951...e01eb9 )
by Jared
02:09
created

BaseCache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 104
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 5 2
A fetch() 0 11 2
A remove() 0 9 2
A exists() 0 4 1
A getTempFile() 0 8 2
A createTempFile() 0 7 1
A encode() 0 4 1
A decode() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 03/08/2017
6
 * Time: 17:17
7
 */
8
9
namespace JC;
10
11
use ReflectionClass;
12
13
/**
14
 * Class SimpleCache
15
 * @package JC
16
 */
17
class BaseCache
18
{
19
    /**
20
     * @param $key
21
     * @param $data
22
     * @return bool
23
     *
24
     * Add object to cache list and save object as json file
25
     */
26
    public static function add($key, $data, $ttl = 0)
27
    {
28
        $tempFilePath = static::getTempFile($key) ?: static::createTempFile($key, $ttl);
29
        return (bool)file_put_contents($tempFilePath, static::encode($data));
30
    }
31
32
    /**
33
     * @param $key
34
     * @param string $className
35
     * @return object|bool
36
     *
37
     * Fetch object from cache
38
     */
39
    public static function fetch($key, $className)
40
    {
41
        if (BaseManager::has($key)) {
42
            $dataString = file_get_contents(static::getTempFile($key));
43
44
            $mapper = new JCMapper();
45
            return $mapper->map(static::decode($dataString), (new ReflectionClass($className))->newInstanceWithoutConstructor());
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * @param $key
53
     * @return bool
54
     *
55
     * Remove object from cache
56
     */
57
    public static function remove($key)
58
    {
59
        if (BaseManager::has($key)) {
60
            unlink(BaseManager::get($key));
61
            return BaseManager::remove($key);
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * @param $key
69
     * @return bool
70
     *
71
     * Check object is cached or not
72
     */
73
    public static function exists($key)
74
    {
75
        return file_exists(static::getTempFile($key));
76
    }
77
78
    /**
79
     * @param $key
80
     * @return bool|string
81
     */
82
    protected static function getTempFile($key)
83
    {
84
        if (BaseManager::has($key)) {
85
            return BaseManager::get($key);
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * @param $key
93
     * @return bool|string
94
     */
95
    protected static function createTempFile($key, $ttl)
96
    {
97
        $tempFilePath = tempnam(sys_get_temp_dir(), $key);
98
        BaseManager::set($key, $tempFilePath, $ttl);
99
100
        return $tempFilePath;
101
    }
102
103
    /**
104
     * @param object $object
105
     * @return string
106
     */
107
    protected static function encode($object)
108
    {
109
        return json_encode($object);
110
    }
111
112
    /**
113
     * @param string $string
114
     * @return object
115
     */
116
    protected static function decode($string)
117
    {
118
        return json_decode($string);
119
    }
120
}