CmsCache::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2015 Oleksandr Torosh (http://yonastudio.com)
4
 * @author Oleksandr Torosh <[email protected]>
5
 */
6
namespace Application\Mvc\Helper;
7
8
use Phalcon\Mvc\User\Component;
9
10
class CmsCache extends Component
11
{
12
13
    private static $instance = null;
14
15
    const DIR = '/../data/cache/cms/';
16
17
    public static function getInstance()
18
    {
19
        if (!self::$instance) {
20
            self::$instance = new CmsCache();
21
        }
22
        return self::$instance;
23
    }
24
25
    public function has($key)
26
    {
27
        if (is_file($this->file($key))) {
28
            return true;
29
        }
30
    }
31
32
    public function get($key)
33
    {
34
        $file = $this->file($key);
35
        if (is_file($file)) {
36
            return json_decode(file_get_contents($file), true);
37
        }
38
    }
39
40
    public function save($key, $data)
41
    {
42
        try {
43
            file_put_contents($this->file($key), json_encode($data));
44
        } catch (\Exception $e) {
45
            $this->flash->error($e->getMessage());
46
        }
47
    }
48
49
    private function file($key)
50
    {
51
        return APPLICATION_PATH . self::DIR . $key . '.json';
52
    }
53
54
}