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

AdapterFile::get()   B

Complexity

Conditions 11
Paths 17

Size

Total Lines 46

Duplication

Lines 7
Ratio 15.22 %

Code Coverage

Tests 21
CRAP Score 11.0795

Importance

Changes 0
Metric Value
dl 7
loc 46
ccs 21
cts 23
cp 0.913
rs 7.3166
c 0
b 0
f 0
cc 11
nc 17
nop 1
crap 11.0795

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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