Completed
Push — master ( 579110...b52e58 )
by Pierre
02:10
created

TFileCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCachePath() 0 3 1
A setCache() 0 6 1
A getCacheFilename() 0 8 2
A cacheExpired() 0 6 2
A clearCache() 0 12 3
A getCache() 0 3 1
1
<?php
2
3
namespace App\Reuse\Controllers\Api;
4
5
trait TFileCache
6
{
7
8
    protected $cacheTtl = 60 * 5;
9
    protected $cacheFilename = '';
10
11
    /**
12
     * return true if cache is expired
13
     *
14
     * @return boolean
15
     */
16
    protected function cacheExpired(): bool
17
    {
18
        $this->cacheFilename = $this->getCacheFilename();
19
        return (file_exists($this->cacheFilename) === false)
20
            ? true
21
            : filemtime($this->cacheFilename) < (time() - $this->cacheTtl);
22
    }
23
24
    /**
25
     * returns cache content
26
     *
27
     * @return string
28
     */
29
    protected function getCache(): string
30
    {
31
        return file_get_contents($this->cacheFilename);
32
    }
33
34
    /**
35
     * set cache content
36
     *
37
     * @param string $content
38
     * @return integer
39
     */
40
    protected function setCache(string $content): int
41
    {
42
        return file_put_contents(
43
            $this->cacheFilename,
44
            $content,
45
            LOCK_EX
46
        );
47
    }
48
49
    /**
50
     * clear cache dir
51
     *
52
     * @param bool $fromRequest
53
     * @return void
54
     */
55
    protected function clearCache(bool $fromRequest = false)
56
    {
57
        if ($fromRequest) {
58
            @unlink($this->getCacheFilename());
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

58
            /** @scrutinizer ignore-unhandled */ @unlink($this->getCacheFilename());

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...
59
        } else {
60
            $fileList = array_filter(
61
                glob($this->getCachePath() . '*'),
0 ignored issues
show
Bug introduced by
It seems like glob($this->getCachePath() . '*') can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

61
                /** @scrutinizer ignore-type */ glob($this->getCachePath() . '*'),
Loading history...
62
                'is_file'
63
            );
64
            $counter = count($fileList);
65
            for ($c = 0; $c < $counter; $c++) {
66
                unlink($fileList[$c]);
67
            }
68
        }
69
    }
70
71
    /**
72
     * returns cache filename from request uri
73
     *
74
     * @return string
75
     */
76
    protected function getCacheFilename(): string
77
    {
78
        $path = $this->getCachePath();
79
        if (!file_exists($path)) {
80
            mkdir($path);
81
        }
82
        $filename = md5($this->request->getUri());
83
        return $path . $filename;
84
    }
85
86
    /**
87
     * returns cache path from request script filename
88
     *
89
     * @return string
90
     */
91
    protected function getCachePath(): string
92
    {
93
        return dirname($this->request->getFilename()) . '/../cache/';
94
    }
95
}
96