FileHandler::destroy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Session
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Session\Handler;
16
17
use SessionHandlerInterface;
18
use Phossa2\Shared\Base\ObjectAbstract;
19
20
/**
21
 * FileHandler
22
 *
23
 * Simple file handler for session
24
 *
25
 * @package Phossa2\Session
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ObjectAbstract
28
 * @see     SessionHandlerInterface
29
 * @version 2.1.0
30
 * @since   2.1.0 added
31
 */
32
class FileHandler extends ObjectAbstract implements SessionHandlerInterface
33
{
34
    /**
35
     * storage path
36
     *
37
     * @var    string
38
     * @access protected
39
     */
40
    protected $path;
41
42
    /**
43
     * session file prefix
44
     *
45
     * @var    string
46
     * @access protected
47
     */
48
    protected $prefix;
49
50
    /**
51
     * Constructor
52
     *
53
     * @param  string $path
54
     * @access public
55
     */
56
    public function __construct($path = '', $prefix = 'sess_')
57
    {
58
        $this->prefix = $prefix;
59
        if (empty($path)) {
60
            $this->path = sys_get_temp_dir() . '/session/';
61
        } else {
62
            $this->path = rtrim($path, '/') . '/';
63
        }
64
65
        if (!is_dir($this->path)) {
66
            mkdir($this->path, 0777, true);
67
        }
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     *
73
     * @return bool
74
     * @see    SessionHandlerInterface::close()
75
     */
76
    public function close()/*# : bool */
77
    {
78
        return true;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     *
84
     * @param  string $session_id
85
     * @return bool
86
     * @see    SessionHandlerInterface::destroy()
87
     */
88
    public function destroy(/*# string */ $session_id)/*# : bool */
89
    {
90
        $file = $this->getSessionFile($session_id);
91
        return unlink($file);
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     *
97
     * @param  int $maxlifetime
98
     * @return bool
99
     * @see    SessionHandlerInterface::gc()
100
     */
101
    public function gc(/*# int */ $maxlifetime)/*# : bool */
102
    {
103
        return true;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     *
109
     * @param  string $save_path
110
     * @param  string $session_name
111
     * @return bool
112
     * @see    SessionHandlerInterface::open()
113
     */
114
    public function open(
115
        /*# string */ $save_path,
116
        /*# string */ $session_name
117
    )/*# : bool */ {
118
        return true;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     *
124
     * @param  string $session_id
125
     * @return string
126
     * @see    SessionHandlerInterface::read()
127
     */
128
    public function read(/*# string */ $session_id)/*# : string */
129
    {
130
        $file = $this->getSessionFile($session_id);
131
        $content = @file_get_contents($file);
132
        if (false === $content) {
133
            $content = 'a:0:{}';
134
        }
135
        return $content;
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     *
141
     * @param  string $session_id
142
     * @param  string $session_data
143
     * @return bool
144
     * @see    SessionHandlerInterface::write()
145
     */
146
    public function write(
147
        /*# string */ $session_id,
148
        /*# string */ $session_data
149
    )/*# : bool */ {
150
        $file = $this->getSessionFile($session_id);
151
        return file_put_contents($file, $session_data) !== false;
152
    }
153
154
    /**
155
     * Return the file path
156
     *
157
     * @param  string $session_id
158
     * @return string
159
     * @access protected
160
     */
161
    protected function getSessionFile(/*# string */ $session_id)/*# : string */
162
    {
163
        return $this->path . $this->prefix . $session_id;
164
    }
165
}
166