Completed
Push — master ( 5c6447...c86f07 )
by Pierre
02:59
created

TFileCache::clearCache()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.3906

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 11
c 2
b 0
f 1
nc 6
nop 1
dl 0
loc 15
ccs 9
cts 12
cp 0.75
crap 5.3906
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Reuse\Controllers\Api;
6
7
trait TFileCache
8
{
9
10
    protected $cacheTtl = 60 * 5;
11
    protected $cacheFilename = '';
12
13
    /**
14
     * return true if cache is expired
15
     *
16
     * @return boolean
17
     */
18 1
    protected function cacheExpired(): bool
19
    {
20 1
        $this->cacheFilename = $this->getCacheFilename();
21 1
        return (file_exists($this->cacheFilename) === false)
22 1
            ? true
23 1
            : filemtime($this->cacheFilename) < (time() - $this->cacheTtl);
24
    }
25
26
    /**
27
     * returns cache content
28
     *
29
     * @return string
30
     */
31 1
    protected function getCache(): string
32
    {
33 1
        return file_get_contents($this->cacheFilename);
34
    }
35
36
    /**
37
     * set cache content
38
     *
39
     * @param string $content
40
     * @return integer
41
     */
42 1
    protected function setCache(string $content): int
43
    {
44 1
        return file_put_contents(
45 1
            $this->cacheFilename,
46
            $content,
47 1
            LOCK_EX
48
        );
49
    }
50
51
    /**
52
     * clear cache dir
53
     *
54
     * @param bool $fromRequest
55
     * @return void
56
     */
57 1
    protected function clearCache(bool $fromRequest = false)
58
    {
59 1
        if ($fromRequest) {
60
            $filename = $this->getCacheFilename();
61
            if (is_writable($filename)) {
62
                @unlink($filename);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

62
                /** @scrutinizer ignore-unhandled */ @unlink($filename);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
            }
64
        } else {
65 1
            $files = glob($this->getCachePath() . '*');
66 1
            $fileList = is_array($files)
67 1
                ? array_filter($files, 'is_file')
68 1
                : [];
69 1
            $counter = count($fileList);
70 1
            for ($c = 0; $c < $counter; $c++) {
71 1
                unlink($fileList[$c]);
72
            }
73
        }
74
    }
75
76
    /**
77
     * returns cache filename from request uri
78
     *
79
     * @return string
80
     */
81 1
    protected function getCacheFilename(): string
82
    {
83 1
        $path = $this->getCachePath();
84 1
        if (!file_exists($path)) {
85
            mkdir($path);
86
        }
87 1
        $filename = md5($this->request->getUri());
88 1
        return $path . $filename;
89
    }
90
91
    /**
92
     * returns cache path from request script filename
93
     *
94
     * @return string
95
     */
96 1
    protected function getCachePath(): string
97
    {
98 1
        return dirname($this->request->getFilename()) . '/../cache/';
99
    }
100
}
101