Completed
Push — master ( 67b201...64a3e6 )
by Lars
02:31 queued 11s
created

AdapterFile::setExpired()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 15
cts 15
cp 1
rs 9.1288
c 0
b 0
f 0
cc 5
nc 4
nop 3
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterFile: File-adapter
9
 */
10
class AdapterFile extends AdapterFileAbstract
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 7
    public function get(string $key)
16
    {
17 7
        $path = $this->getFileName($key);
18
19 View Code Duplication
        if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20 7
            \file_exists($path) === false
21
            ||
22 7
            \filesize($path) === 0
23
        ) {
24 1
            return null;
25
        }
26
27
        // init
28 7
        $string = '';
29
30
        /** @noinspection PhpUsageOfSilenceOperatorInspection */
31 7
        $fp = @\fopen($path, 'rb');
32 7
        if ($fp && \flock($fp, \LOCK_SH | \LOCK_NB)) {
33 7
            while (!\feof($fp)) {
34 7
                $line = \fgets($fp);
35 7
                $string .= $line;
36
            }
37 7
            \flock($fp, \LOCK_UN);
38
        }
39 7
        if ($fp) {
40 7
            \fclose($fp);
41
        }
42
43 7
        if (!$string) {
44
            return null;
45
        }
46
47 7
        $data = $this->serializer->unserialize($string);
48
49 7
        if (!$data || !$this->validateDataFromCache($data)) {
50
            return null;
51
        }
52
53 7
        if ($this->ttlHasExpired($data['ttl']) === true) {
54 2
            $this->remove($key);
55
56 2
            return null;
57
        }
58
59 6
        return $data['value'];
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 6
    public function setExpired(string $key, $value, int $ttl = 0): bool
66
    {
67 6
        $item = $this->serializer->serialize(
68
            [
69 6
                'value' => $value,
70 6
                'ttl'   => $ttl ? $ttl + \time() : 0,
71
            ]
72
        );
73
74
        // init
75 6
        $octetWritten = false;
76
77 6
        $cacheFile = $this->getFileName($key);
78
79
        // Open the file for writing only. If the file does not exist, it is created.
80
        // If it exists, it is neither truncated, nor the call to this function fails.
81
        /** @noinspection PhpUsageOfSilenceOperatorInspection */
82 6
        $fp = @\fopen($cacheFile, 'cb');
83 6
        if ($fp && \flock($fp, \LOCK_EX | \LOCK_NB)) {
84 6
            \ftruncate($fp, 0);
85 6
            $octetWritten = \fwrite($fp, $item);
86 6
            \fflush($fp);
87 6
            \flock($fp, \LOCK_UN);
88
        }
89 6
        if ($fp !== false) {
90 6
            \fclose($fp);
91
        }
92
93 6
        return $octetWritten !== false;
94
    }
95
}
96