BaseCache::getTempFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 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\Cache;
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
     * @param bool $isArray
36
     * @return object|mixed|bool
37
     *
38
     * Fetch data from cache
39
     */
40
    public static function fetch($key, $className = '', $isArray = false)
41
    {
42
        if (Manager::has($key)) {
43
            $dataString = file_get_contents(static::getTempFile($key));
44
            $rawData = static::decode($dataString);
45
46
            if (!empty($className)) {
47
                $mapper = new JCMapper();
48
                return $mapper->map($rawData, (new ReflectionClass($className))->newInstanceWithoutConstructor());
49
            } else {
50
                return $isArray ? (array)$rawData : $rawData;
51
            }
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * @param $key
59
     * @return array
60
     *
61
     * Fetch array from cache
62
     */
63
    public static function fetchArray($key)
64
    {
65
        return static::fetch($key, '', true);
66
    }
67
68
    /**
69
     * @param $key
70
     * @return bool
71
     *
72
     * Remove object from cache
73
     */
74
    public static function remove($key)
75
    {
76
        if (Manager::has($key)) {
77
            unlink(Manager::get($key));
78
            return Manager::remove($key);
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * @param $key
86
     * @return bool
87
     *
88
     * Check object is cached or not
89
     */
90
    public static function exists($key)
91
    {
92
        return file_exists(static::getTempFile($key));
93
    }
94
95
    /**
96
     * @param $key
97
     * @return bool|string
98
     */
99
    protected static function getTempFile($key)
100
    {
101
        if (Manager::has($key)) {
102
            return Manager::get($key);
103
        }
104
105
        return false;
106
    }
107
108
    /**
109
     * @param $key
110
     * @return bool|string
111
     */
112
    protected static function createTempFile($key, $ttl)
113
    {
114
        $tempFilePath = tempnam(sys_get_temp_dir(), $key);
115
        Manager::set($key, $tempFilePath, $ttl);
116
117
        return $tempFilePath;
118
    }
119
120
    /**
121
     * @param object $object
122
     * @return string
123
     */
124
    protected static function encode($object)
125
    {
126
        return json_encode($object);
127
    }
128
129
    /**
130
     * @param string $string
131
     * @return object
132
     */
133
    protected static function decode($string)
134
    {
135
        return json_decode($string);
136
    }
137
}