Completed
Pull Request — master (#72)
by
unknown
02:34
created

Cache::cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace AmoCRM\Models;
4
5
/**
6
 * Class Cache
7
 */
8
9
class Cache
10
{
11
    private $cache_path = '';
12
    private $expire = null;
13
14 2
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
    {
16 2
        $this->cache_path = defined('CACHE_DIR')?CACHE_DIR:sys_get_temp_dir();
17 2
        $this->expire = defined('CACHE_EXPIRE')?CACHE_EXPIRE:'3600';
18 2
    }
19
20
    function cache(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
21
22
    }
23
24
    function setCache($name,$data){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
        $this->writeFile($name,json_encode($data));
26
        return $data;
27
    }
28
29 2
    function getCache($name, $expire=null){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30 2
        if(!is_null($expire)){
31
            $this->expire = $expire;
32
        }
33
34 2
        $cached = $this->readFile($name);
35
36
        if($cached !== false){
37
            $data = json_decode($cached, true);
38
            $cache_time = $data['server_time'];
39
            $now_time = time();
40
            if($now_time >= $cache_time && $now_time <= ($cache_time + $this->expire)){
41
                return $data;
42
            }
43
        }
44
        return false;
45
    }
46
47
    function writeFile($name, $content){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
        try {
49
            if(!is_dir($this->cache_path)) {
50
                mkdir($this->cache_path);
51
            }
52
            return file_put_contents($this->cache_path."/".$name,$content);
53
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class AmoCRM\Models\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
54
            // Handle exception
55
            return false;
56
        }
57
    }
58
59 2
    function readFile($name){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
60
        try {
61 2
            $content = file_get_contents($this->cache_path."/".$name, false);
62
            if ($content === false) {
63
                // Handle the error
64
            } else {
65
                return $content;
66
            }
67 2
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class AmoCRM\Models\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
68
            // Handle exception
69
            return false;
70
        }
71
    }
72
}
73