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

Cache   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 27.03%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 64
ccs 10
cts 37
cp 0.2703
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A cache() 0 3 1
A setCache() 0 4 1
A getCache() 0 17 5
A writeFile() 0 11 3
A readFile() 0 13 3
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