Adapter::setFromStorage()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 10
cp 0.8
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5.2
1
<?php
2
3
namespace League\Flysystem\Cached\Storage;
4
5
use League\Flysystem\AdapterInterface;
6
use League\Flysystem\Config;
7
8
class Adapter extends AbstractCache
9
{
10
    /**
11
     * @var AdapterInterface An adapter
12
     */
13
    protected $adapter;
14
15
    /**
16
     * @var string the file to cache to
17
     */
18
    protected $file;
19
20
    /**
21
     * @var int|null seconds until cache expiration
22
     */
23
    protected $expire = null;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param AdapterInterface $adapter adapter
29
     * @param string           $file    the file to cache to
30
     * @param int|null         $expire  seconds until cache expiration
31
     */
32 21
    public function __construct(AdapterInterface $adapter, $file, $expire = null)
33
    {
34 21
        $this->adapter = $adapter;
35 21
        $this->file = $file;
36 21
        $this->setExpire($expire);
37 21
    }
38
39
    /**
40
     * Set the expiration time in seconds.
41
     *
42
     * @param int $expire relative expiration time
43
     */
44 21
    protected function setExpire($expire)
45
    {
46 21
        if ($expire) {
47 9
            $this->expire = $this->getTime($expire);
48 9
        }
49 21
    }
50
51
    /**
52
     * Get expiration time in seconds.
53
     *
54
     * @param int $time relative expiration time
55
     *
56
     * @return int actual expiration time
57
     */
58 9
    protected function getTime($time = 0)
59
    {
60 9
        return intval(microtime(true)) + $time;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 9
    public function setFromStorage($json)
67
    {
68 9
        list($cache, $complete, $expire) = json_decode($json, true);
69
70 9
        if (! $expire || $expire > $this->getTime()) {
71 6
            $this->cache = is_array($cache) ? $cache : [];
72 6
            $this->complete = is_array($complete) ? $complete : [];
73 6
        } else {
74 3
            $this->adapter->delete($this->file);
75
        }
76 9
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 12
    public function load()
82
    {
83 12
        if ($this->adapter->has($this->file)) {
84 9
            $file = $this->adapter->read($this->file);
85 9
            if ($file && !empty($file['contents'])) {
86 9
                $this->setFromStorage($file['contents']);
87 9
            }
88 9
        }
89 12
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 12
    public function getForStorage()
95
    {
96 12
        $cleaned = $this->cleanContents($this->cache);
97
98 12
        return json_encode([$cleaned, $this->complete, $this->expire]);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 12
    public function save()
105
    {
106 12
        $config = new Config();
107 12
        $contents = $this->getForStorage();
108
109 12
        if ($this->adapter->has($this->file)) {
110 6
            $this->adapter->update($this->file, $contents, $config);
111 6
        } else {
112 6
            $this->adapter->write($this->file, $contents, $config);
113
        }
114 12
    }
115
}
116