Passed
Push — master ( 9385e3...10d00a )
by Alexey
04:48
created

Cache::connect()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 12
rs 9.4285
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) {
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
                $val = $callback($params);
71
                self::set($name, $params, $val);
72
                return $val;
73
            }
74
        }
75
        return false;
76
    }
77
78
    /**
79
     * Set value to cache
80
     * 
81
     * @param string $name
82
     * @param array $params
83
     * @param mixed $val
84
     * @param int $lifeTime
85
     * @return boolean
86
     */
87
    public static function set($name, $params = [], $val = '', $lifeTime = 3600) {
88
        if (!self::$connected) {
89
            self::connect();
90
        }
91
        if (self::$connected) {
92
            return @self::$server->set($name . serialize($params), $val, false, $lifeTime);
93
        }
94
        return false;
95
    }
96
97
    /**
98
     * Move file to cache folder and return path
99
     * 
100
     * Also resize image when given resize params
101
     * 
102
     * @param string $file
103
     * @param array $options
104
     * @return string
105
     */
106
    public static function file($file, $options = []) {
107
        $sizes = !empty($options['resize']) ? $options['resize'] : [];
108
        $crop = !empty($options['crop']) ? $options['crop'] : '';
109
        $pos = !empty($options['pos']) ? $options['pos'] : 'center';
110
        $fileinfo = pathinfo($file);
111
        $fileCheckSum = md5($fileinfo['dirname'] . filemtime($file));
112
        $path = static::getDir('static') . $fileCheckSum . '_' . $fileinfo['filename'];
113
        if ($sizes) {
114
            $path .= '.' . $sizes['x'] . 'x' . $sizes['y'] . $crop . $pos;
115
        }
116
        $path .= '.' . $fileinfo['extension'];
117
        if (!file_exists($path)) {
118
            copy($file, $path);
119
            if ($sizes) {
120
                Tools::resizeImage($path, $sizes['x'], $sizes['y'], $crop, $pos);
121
            }
122
        }
123
124
        return $path;
125
    }
126
127
    /**
128
     * Get cache dir for app
129
     * 
130
     * @param App $app
131
     * @return string
132
     */
133
    public static function getDir($dirname, $app = null) {
134
        if (!$app) {
135
            $app = App::$primary;
136
        }
137
        $path = static::folder() . "{$dirname}/{$app->dir}/";
138
        Tools::createDir($path);
139
        return $path;
140
    }
141
142
    public static function folder() {
143
        return 'cache/';
144
    }
145
}