Completed
Push — master ( 097ee4...b7f9bf )
by Lars
02:18
created

AdapterFile::createCacheDirectory()   C

Complexity

Conditions 13
Paths 11

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 14.1683

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 17
cts 21
cp 0.8095
rs 6.6166
c 0
b 0
f 0
cc 13
nc 11
nop 1
crap 14.1683

1 Method

Rating   Name   Duplication   Size   Complexity  
A AdapterFile::setExpired() 0 28 4

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