Completed
Push — master ( b8b30b...725bad )
by Miles
02:44
created

StashSessionHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 12
c 3
b 1
f 0
lcom 1
cbo 2
dl 0
loc 116
ccs 7
cts 7
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A open() 0 4 1
A close() 0 4 1
A read() 0 7 2
A write() 0 7 1
A destroy() 0 6 1
A gc() 0 4 1
A getPool() 0 4 1
A getItem() 0 4 1
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
    /**
129
     * Return a Stash Pool item
130
     *
131
     * @param string $sessionId The sessionid
132
     *
133
     * @return \Stash\Item
134
     *
135
     * @codeCoverageIgnore
136
     */
137
    private function getItem($sessionId)
138
    {
139
        return $this->pool->getItem(sprintf('%s/%s', $this->prefix, $sessionId));
140
    }
141
}
142