File::set()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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