CustomCache   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 7
A getData() 0 15 5
1
<?php
2
3
/**
4
 * Simple class that helps caching data
5
 * created mostly for illustrative purposes
6
 *
7
 * Usage:
8
 * $cache = new CustomCache(360, 'some_remote_data', function(){
9
 *      return file_get_contents('http://imagecms.net/some_controller/get_some_xml');
10
 * });
11
 *
12
 * $myXml = $cache->getData(); // you don't know if data was from cache, or from remote source
13
 *
14
 * @author kolia
15
 */
16
class CustomCache
17
{
18
19
    const CACHE_PATH = 'system/cache/';
20
21
    protected static $cacheFilenames = [];
22
23
    /**
24
     * Interval in seconds
25
     * @var int
26
     */
27
    protected $interval;
28
29
    /**
30
     *
31
     * @var string
32
     */
33
    protected $cacheFilepath;
34
35
    /**
36
     * Function that returns the actual data
37
     * @var Closure
38
     */
39
    protected $dataSourceCallback;
40
41
    /**
42
     *
43
     * @param int $interval update interval in seconds
44
     * @param string $cacheFilename file name that will be stored cache
45
     * @param Closure $dataSourceCallback function that returns actual data
46
     * @throws \Exception
47
     */
48
    public function __construct($interval = 0, $cacheFilename, Closure $dataSourceCallback) {
49
        if (is_numeric($interval)) {
50
            $this->interval = $interval;
0 ignored issues
show
Documentation Bug introduced by
It seems like $interval can also be of type double or string. However, the property $interval is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
        } elseif (ENVIRONMENT == 'development') {
52
            throw new \Exception(lang('Interval argument must be integer', 'main'));
53
        }
54
55
        $fileError = '';
56
        if (!preg_match('/^[a-zA-Z0-9\_\-]{1,240}$/', $cacheFilename)) {
57
            $fileError = lang('Cache file name is not valid string. ', 'main');
58
        }
59
        if (in_array($cacheFilename, self::$cacheFilenames)) {
60
            $fileError = lang('Cache file with such name already exist. Please chose another name', 'main');
61
        }
62
63
        if ($fileError === '') {
64
            $this->cacheFilepath = PUBPATH . self::CACHE_PATH . 'custom_cache_' . $cacheFilename . '.txt';
65
            self::$cacheFilenames[] = $cacheFilename;
66
        } elseif (ENVIRONMENT == 'development') {
67
            throw new \Exception($fileError);
68
        }
69
70
        $this->dataSourceCallback = $dataSourceCallback;
71
    }
72
73
    /**
74
     * Gets the data (client don't know it's from cache or not)
75
     * @return string
76
     */
77
    public function getData() {
78
        $dataSourceCallback = $this->dataSourceCallback;
79
        // if it's production and configuratin has errors data always will be from source
80
        if ($this->cacheFilepath == null || $this->dataSourceCallback == null) {
81
            return $dataSourceCallback();
82
        }
83
        // if chached file exists and has actual data it's content will be returned
84
        if (file_exists($this->cacheFilepath) && time() < (filemtime($this->cacheFilepath) + $this->interval)) {
85
            return file_get_contents($this->cacheFilepath);
86
        }
87
        // getting new data, save it, return it =)
88
        $data = $dataSourceCallback();
89
        file_put_contents($this->cacheFilepath, $data);
90
        return $data;
91
    }
92
93
}