Passed
Push — develop ( 7bbca0...8be0b9 )
by Mathieu
01:38
created

File::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
namespace Suricate\Cache;
3
4
use Suricate;
5
6
/**
7
 * Memcache extension for Suricate
8
 *
9
 * @package Suricate
10
 * @author  Mathieu LESNIAK <[email protected]>
11
 *
12
 * @property string $path           Storage path (default: app/storage/app)
13
 * @property int    $defaultExpiry  Key default expiry in sec
14
 */
15
16
class File extends Suricate\Cache
17
{
18
    protected $parametersList = [
19
        'path',
20
        'defaultExpiry',
21
    ];
22
    private $handler;
23
24 2
    public function __construct()
25
    {
26 2
        parent::__construct();
27
28 2
        $this->handler          = false;
29 2
        $this->path             = app_path() . '/storage/app/';
30 2
        $this->defaultExpiry    = 3600;
31 2
    }
32
33 1
    public function getDefaultExpiry()
34
    {
35 1
        return $this->defaultExpiry;
36
    }
37
38 1
    public function setDefaultExpiry($expiry)
39
    {
40 1
        $this->defaultExpiry = $expiry;
41
42 1
        return $this;
43
    }
44
    
45
    /**
46
     * Put a value into memcache
47
     * @param string $variable Variable name
48
     * @param mixed $value    Value
49
     * @param int $expiry   Cache expiry
50
     */
51
    public function set(string $variable, $value, $expiry = null)
52
    {
53
        if ($expiry === null) {
54
            $expiry = $this->defaultExpiry;
55
        }
56
        $fp = fopen($this->path . $variable, 'w');
57
        if ($fp === false) {
58
            throw new \Exception("Cannot open cache file " . $this->path . $variable);
59
        }
60
        fputs($fp, $value);
61
        fclose($fp);
62
        if ($expiry !== null) {
0 ignored issues
show
introduced by
The condition $expiry !== null is always true.
Loading history...
63
            $fp = fopen($this->path . $variable .'.expiry', 'w');
64
            if ($fp === false) {
65
                throw new \Exception("Cannot open cache file " . $this->path . $variable .'.expiry');
66
            }
67
            fputs($fp, (string) (time() + $expiry));
68
            fclose($fp);
69
        }
70
    }
71
72
    public function get(string $variable)
73
    {
74
        if (is_readable($this->path . $variable)) {
75
            if (is_readable($this->path . $variable . '.expiry')) {
76
                $expiry = file_get_contents($this->path . $variable . '.expiry');
77
                $hasExpired = (time() - $expiry) > 0 ? 1 : -1;
78
            } else {
79
                $hasExpired = 0;
80
            }
81
            
82
            if ($hasExpired < 0) {
83
                return file_get_contents($this->path . $variable);
84
            } elseif ($hasExpired > 0) {
85
                unlink($this->path . $variable . '.expiry');
86
            }
87
        }
88
        return null;
89
    }
90
91
    public function delete(string $variable)
92
    {
93
        if (is_file($this->path . $variable)) {
94
            return unlink($this->path . $variable);
95
        }
96
        if (is_file($this->path . $variable . '.expiry')) {
97
            return unlink($this->path . $variable . '.expiry');
98
        }
99
100
        return false;
101
    }
102
}
103