Passed
Push — master ( 3c979b...72c793 )
by Kirill
03:02
created

FileHandler::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Session\Handler;
13
14
use Spiral\Files\FilesInterface;
15
16
/**
17
 * Stores session data in file.
18
 */
19
final class FileHandler implements \SessionHandlerInterface
20
{
21
    /**
22
     * @var FilesInterface
23
     */
24
    protected $files;
25
26
    /**
27
     * @var string
28
     */
29
    protected $directory = '';
30
31
    /**
32
     * @param FilesInterface $files
33
     * @param string         $directory
34
     * @param int            $lifetime Default session lifetime.
35
     */
36
    public function __construct(FilesInterface $files, string $directory, int $lifetime = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $lifetime is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
    public function __construct(FilesInterface $files, string $directory, /** @scrutinizer ignore-unused */ int $lifetime = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
        $this->files = $files;
39
        $this->directory = $directory;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function close(): bool
46
    {
47
        return true;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     * @codeCoverageIgnore
53
     */
54
    public function destroy($session_id): bool
55
    {
56
        return $this->files->delete($this->getFilename($session_id));
57
    }
58
59
    /**
60
     * @inheritdoc
61
     * @codeCoverageIgnore
62
     */
63
    public function gc($maxlifetime): void
64
    {
65
        foreach ($this->files->getFiles($this->directory) as $filename) {
66
            if ($this->files->time($filename) < time() - $maxlifetime) {
67
                $this->files->delete($filename);
68
            }
69
        }
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function open($save_path, $session_id): bool
76
    {
77
        return true;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function read($session_id): string
84
    {
85
        return $this->files->exists($this->getFilename($session_id))
86
            ? $this->files->read($this->getFilename($session_id))
87
            : '';
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function write($session_id, $session_data): bool
94
    {
95
        return $this->files->write(
96
            $this->getFilename($session_id),
97
            $session_data,
98
            FilesInterface::RUNTIME,
99
            true
100
        );
101
    }
102
103
    /**
104
     * Session data filename.
105
     *
106
     * @param string $session_id
107
     *
108
     * @return string
109
     */
110
    protected function getFilename($session_id): string
111
    {
112
        return "{$this->directory}/{$session_id}";
113
    }
114
}
115