FileStore   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 70
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 0 18 3
A save() 0 9 1
A delete() 0 4 1
A getFileName() 0 4 1
1
<?php
2
namespace Sesshin\Store;
3
4
class FileStore implements StoreInterface
5
{
6
    /** @var string */
7
    protected $dir;
8
9
    /**
10
     * @param string
11
     */
12
    public function __construct($dir)
13
    {
14
        $this->dir = $dir;
15
    }
16
17
    /**
18
     * @param string $id
19
     * @return bool|mixed
20
     */
21
    public function fetch($id)
22
    {
23
        $fileName = $this->getFileName($id);
24
25
        if (file_exists($fileName)) {
26
            list($expirationTime, $content) = explode('|', file_get_contents($fileName));
27
28
            if ($expirationTime < time()) {
29
                $this->delete($id);
30
31
                return false;
32
            }
33
34
            return unserialize($content);
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * @param string $id
42
     * @param mixed $data
43
     * @param int $lifeTime
44
     * @return int
45
     */
46
    public function save($id, $data, $lifeTime)
47
    {
48
        $fileName = $this->getFileName($id);
49
50
        $expirationTime = time() + $lifeTime;
51
        $content = $expirationTime . '|' . serialize($data);
52
53
        return file_put_contents($fileName, $content);
54
    }
55
56
    /**
57
     * @param string $id
58
     * @return bool
59
     */
60
    public function delete($id)
61
    {
62
        return unlink($this->getFileName($id));
63
    }
64
65
    /**
66
     * @param string $id
67
     * @return string
68
     */
69
    protected function getFileName($id)
70
    {
71
        return $this->dir . DIRECTORY_SEPARATOR . $id . '.sess';
72
    }
73
}
74