Completed
Push — master ( 9db94b...c8aa88 )
by Alexey
07:41
created

Cache::connect()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 3
eloc 5
nc 2
nop 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
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
    {
39
        if (!self::$connectTrying && class_exists('Memcache', false)) {
40
            self::$server = new Memcache();
41
            self::$connected = @self::$server->connect('localhost', 11211);
42
        }
43
        self::$connectTrying = true;
44
    }
45
46
    /**
47
     * Get chached value
48
     * 
49
     * If value not present, call callback
50
     * 
51
     * @param string $name
52
     * @param array $params
53
     * @param callable $callback
54
     * @return boolean
55
     */
56
    public static function get($name, $params = [], $callback = null)
57
    {
58
        if (!self::$connected) {
59
            self::connect();
60
        }
61
        if (!self::$connected) {
62
            if (is_callable($callback, true)) {
63
                return $callback($params);
64
            }
65
            return false;
66
        }
67
        $val = @self::$server->get($name . serialize($params));
68
        if ($val !== false) {
69
            return $val;
70
        } else {
71
            if (is_callable($callback, true)) {
72
                $val = $callback($params);
73
                self::set($name, $params, $val);
74
                return $val;
75
            }
76
        }
77
        return false;
78
    }
79
80
    /**
81
     * Set value to cache
82
     * 
83
     * @param string $name
84
     * @param array $params
85
     * @param mixed $val
86
     * @param int $lifeTime
87
     * @return boolean
88
     */
89
    public static function set($name, $params = [], $val = '', $lifeTime = 3600)
90
    {
91
        if (!self::$connected) {
92
            self::connect();
93
        }
94
        if (self::$connected) {
95
            return @self::$server->set($name . serialize($params), $val, false, $lifeTime);
96
        }
97
        return false;
98
    }
99
100
    /**
101
     * Move file to cache folder and return path
102
     * 
103
     * Also resize image when given resize params
104
     * 
105
     * @param string $file
106
     * @param array $options
107
     * @return string
108
     */
109
    public static function file($file, $options = [])
110
    {
111
        $sizes = !empty($options['resize']) ? $options['resize'] : [];
112
        $crop = !empty($options['crop']) ? $options['crop'] : '';
113
        $pos = !empty($options['pos']) ? $options['pos'] : 'center';
114
        $fileinfo = pathinfo($file);
115
        $fileCheckSum = md5($fileinfo['dirname'] . filemtime($file));
116
        $path = static::getDir() . '/' . $fileCheckSum . '_' . $fileinfo['filename'];
117
        if ($sizes) {
118
            $path .= '.' . $sizes['x'] . 'x' . $sizes['y'] . $crop . $pos;
119
        }
120
        $path .= '.' . $fileinfo['extension'];
121 View Code Duplication
        if (!file_exists($path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
            copy($file, $path);
123
            if ($sizes) {
124
                Tools::resizeImage($path, $sizes['x'], $sizes['y'], $crop, $pos);
125
            }
126
        }
127
128
        return $path;
129
    }
130
131
    /**
132
     * Get cache dir for app
133
     * 
134
     * @param App $app
135
     * @return string
136
     */
137
    public static function getDir($app = null)
138
    {
139
        if (!$app) {
140
            $app = App::$primary;
141
        }
142
        $path = 'cache/' . $app->dir;
143
        Tools::createDir($path);
144
        return $path;
145
    }
146
147
}
148