StashSessionHandler   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 12
c 4
b 1
f 0
lcom 1
cbo 2
dl 0
loc 118
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 9 1
A destroy() 0 6 1
A gc() 0 4 1
A getItem() 0 4 1
A getPool() 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
        $item->expiresAfter($this->ttl);
95
        $item->set($data);
96
97
        return $this->pool->save($item);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     * @codeCoverageIgnore
103
     */
104
    public function destroy($sessionId)
105
    {
106
        $item = $this->getItem($sessionId);
107
108
        return $item->clear();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     * @codeCoverageIgnore
114
     */
115
    public function gc($maxlifetime)
116
    {
117
        return $this->pool->clear();
118
    }
119
120
    /**
121
     * Return a Stash Pool instance.
122
     *
123
     * @return \Stash\Pool
124
     */
125 6
    public function getPool()
126
    {
127 6
        return $this->pool;
128
    }
129
130
    /**
131
     * Return a Stash Pool item
132
     *
133
     * @param string $sessionId The sessionid
134
     *
135
     * @return \Stash\Item
136
     *
137
     * @codeCoverageIgnore
138
     */
139
    private function getItem($sessionId)
140
    {
141
        return $this->pool->getItem(sprintf('%s/%s', $this->prefix, $sessionId));
142
    }
143
}
144