Completed
Push — master ( bfed7d...b8b30b )
by Miles
03:11
created

StashSessionHandler::getItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of the m1\stash-silex library
5
 *
6
 * (c) m1 <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @package     m1/stash-silex
12
 * @version     0.1.0
13
 * @author      Miles Croxford <[email protected]>
14
 * @copyright   Copyright (c) Miles Croxford <[email protected]>
15
 * @license     http://github.com/m1/stashsilex/blob/master/LICENSE
16
 * @link        http://github.com/m1/stashsilex/blob/master/README.MD Documentation
17
 */
18
19
namespace M1\StashSilex;
20
21
/**
22
 * The StashServiceProvider provides the stash service to silex
23
 *
24
 * @since 0.1.0
25
 */
26
class StashSessionHandler implements \SessionHandlerInterface
27
{
28
    /**
29
     * @var \Stash\Pool Stash pool.
30
     */
31
    private $pool;
32
33
    /**
34
     * @var int Time to live in seconds
35
     */
36
    private $ttl;
37
38
    /**
39
     * @var string Key prefix for shared environments.
40
     */
41
    private $prefix;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param \Stash\Pool $pool    A \Stash\Pool instance
47
     * @param array       $options An associative array of Stash options
48
     */
49 12
    public function __construct(\Stash\Pool $pool, array $options = array())
50
    {
51 12
        $this->pool = $pool;
52 12
        $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 1800;
53 12
        $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sessions';
54 12
    }
55
56
    /**
57
     * {@inheritdoc}
58
     * @codeCoverageIgnore
59
     */
60
    public function open($savePath, $sessionName)
61
    {
62
        return true;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     * @codeCoverageIgnore
68
     */
69
    public function close()
70
    {
71
        return true;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     * @codeCoverageIgnore
77
     */
78
    public function read($sessionId)
79
    {
80
        $item = $this->getItem($sessionId);
81
        $data = $item->get();
82
83
        return (!$item->isMiss()) ? $data : '';
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     * @codeCoverageIgnore
89
     */
90
    public function write($sessionId, $data)
91
    {
92
        $item = $this->getItem($sessionId);
93
        $item->lock();
94
95
        return $item->set($data, $this->ttl);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     * @codeCoverageIgnore
101
     */
102
    public function destroy($sessionId)
103
    {
104
        $item = $this->getItem($sessionId);
105
106
        return $item->clear();
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     * @codeCoverageIgnore
112
     */
113
    public function gc($maxlifetime)
114
    {
115
        return $this->pool->purge();
116
    }
117
118
    /**
119
     * Return a Stash Pool instance.
120
     *
121
     * @return \Stash\Pool
122
     */
123 6
    public function getPool()
124
    {
125 6
        return $this->pool;
126
    }
127
128
    private function getItem($sessionId)
129
    {
130
        return $this->pool->getItem(sprintf('%s/%s', $this->prefix, $sessionId));
131
    }
132
}
133