Test Failed
Push — master ( 2d2a45...ddf2cf )
by Alexey
04:46 queued 30s
created

Cache::get()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 22
nc 14
nop 4
dl 0
loc 30
ccs 0
cts 16
cp 0
crap 72
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Cache
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Cache {
12
13
    /**
14
     * Connection to a set of memcache servers
15
     *
16
     * @var Memcache
17
     */
18
    public static $server = null;
19
20
    /**
21
     * Truing to connect flag
22
     *
23
     * @var boolean
24
     */
25
    public static $connectTrying = false;
26
27
    /**
28
     * Connected flag
29
     *
30
     * @var boolean
31
     */
32
    public static $connected = false;
33
34
    /**
35
     * Try connect to memcache server
36
     */
37
    public static function connect() {
38
        if (!self::$connectTrying && class_exists('Memcache', false)) {
39
            self::$server = new Memcache();
40
            self::$connected = @self::$server->connect('localhost', 11211);
41
        }
42
        self::$connectTrying = true;
43
    }
44
45
    /**
46
     * Get chached value
47
     *
48
     * If value not present, call callback
49
     *
50
     * @param string $name
51
     * @param array $params
52
     * @param callable $callback
53
     * @return boolean
54
     */
55
    public static function get($name, $params = [], $callback = null, $lifeTime = 3600) {
56
        if (!self::$connected) {
57
            self::connect();
58
        }
59
        if (!self::$connected) {
60
            if (is_callable($callback, true)) {
61
                return $callback($params);
62
            }
63
            return false;
64
        }
65
        $val = @self::$server->get($name . serialize($params));
66
        if ($val !== false) {
67
            return $val;
68
        } else {
69
            if (is_callable($callback, true)) {
70
                while (!\Inji::$inst->blockParallel()) {
71
                    sleep(1);
72
                    $val = @self::$server->get($name . serialize($params));
73
                    if ($val !== false) {
74
                        return $val;
75
                    }
76
                }
77
                $val = $callback($params);
78
                \Inji::$inst->unBlockParallel();
79
                self::set($name, $params, $val, $lifeTime);
80
                return $val;
81
            }
82
        }
83
        return false;
84
    }
85
86
    /**
87
     * Set value to cache
88
     *
89
     * @param string $name
90
     * @param array $params
91
     * @param mixed $val
92
     * @param int $lifeTime
93
     * @return boolean
94
     */
95
    public static function set($name, $params = [], $val = '', $lifeTime = 3600) {
96
        if (!self::$connected) {
97
            self::connect();
98
        }
99
        if (self::$connected) {
100
            return @self::$server->set($name . serialize($params), $val, false, $lifeTime);
101
        }
102
        return false;
103
    }
104
105
    /**
106
     * Move file to cache folder and return path
107
     *
108
     * Also resize image when given resize params
109
     *
110
     * @param string $file
111
     * @param array $options
112
     * @return string
113
     */
114
    public static function file($file, $options = []) {
115
        $sizes = !empty($options['resize']) ? $options['resize'] : [];
116
        $crop = !empty($options['crop']) ? $options['crop'] : '';
117
        $pos = !empty($options['pos']) ? $options['pos'] : 'center';
118
        $fileinfo = pathinfo($file);
119
        $fileCheckSum = md5($fileinfo['dirname'] . filemtime($file));
120
        $path = static::getDir('static') . $fileCheckSum . '_' . $fileinfo['filename'];
121
        if ($sizes) {
122
            $path .= '.' . $sizes['x'] . 'x' . $sizes['y'] . $crop . $pos;
123
        }
124
        $path .= '.' . $fileinfo['extension'];
125
        if (!file_exists($path)) {
126
            copy($file, $path);
127
            if ($sizes) {
128
                Tools::resizeImage($path, $sizes['x'], $sizes['y'], $crop, $pos);
129
            }
130
        }
131
132
        return $path;
133
    }
134
135
    /**
136
     * Get cache dir for app
137
     *
138
     * @param App $app
139
     * @return string
140
     */
141
    public static function getDir($dirname, $app = null) {
142
        if (!$app) {
143
            $app = App::$primary;
144
        }
145
        $path = static::folder() . "{$dirname}/{$app->dir}/";
146
        Tools::createDir($path);
147
        return $path;
148
    }
149
150
    public static function folder() {
151
        return 'cache/';
152
    }
153
}