Completed
Push — master ( 496bc5...15446f )
by Lars
02:20
created

AdapterOpCache::get()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 27
Code Lines 14

Duplication

Lines 7
Ratio 25.93 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 27
ccs 11
cts 12
cp 0.9167
rs 8.439
cc 6
eloc 14
nc 4
nop 1
crap 6.0208
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterOpCache: PHP-OPcache
9
 *
10
 * OPcache improves PHP performance by storing precompiled script bytecode
11
 * in shared memory, thereby removing the need for PHP to load and
12
 * parse scripts on each request.
13
 *
14
 * @package voku\cache
15
 */
16
class AdapterOpCache extends AdapterFile
17
{
18
  /**
19
   * {@inheritdoc}
20
   */
21 11
  public function __construct($cacheDir = null)
22
  {
23 11
    parent::__construct($cacheDir);
24
25 11
    $this->serializer = new SerializerNo();
26 11
  }
27
28
  /**
29
   * {@inheritdoc}
30
   */
31 6
  public function get(string $key)
32
  {
33 6
    $path = $this->getFileName($key);
34
35 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...
36 6
        \file_exists($path) === false
37
        ||
38 6
        \filesize($path) === 0
39
    ) {
40 1
      return null;
41
    }
42
43
    /** @noinspection PhpIncludeInspection */
44 6
    $data = include $path;
45
46 6
    if (!$data || !$this->validateDataFromCache($data)) {
47
      return null;
48
    }
49
50 6
    if ($this->ttlHasExpired($data['ttl']) === true) {
51 2
      $this->remove($key);
52
53 2
      return null;
54
    }
55
56 5
    return $data['value'];
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62 8
  protected function getFileName(string $key): string
63
  {
64 8
    $result = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . '.php';
65
66 8
    return $result;
67
  }
68
69
  /**
70
   * {@inheritdoc}
71
   */
72 6
  public function setExpired(string $key, $value, int $ttl = 0): bool
73
  {
74
    $item = [
75 6
        'value' => $value,
76 6
        'ttl'   => $ttl ? $ttl + \time() : 0,
77
    ];
78 6
    $content = \var_export($item, true);
79
80
    $content = '<?php
81
    
82
    static $data = [
83 6
      0 => ' . $content . ',
84
    ];
85
    
86
    $result =& $data;
87
    unset($data);
88
    return $result[0];';
89
90 6
    return (bool)\file_put_contents($this->getFileName($key), $content);
91
  }
92
}
93