Completed
Push — master ( 024bcd...cc7886 )
by Changwan
04:26
created

Session::createFromCookieJar()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 4
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Http\Parameters;
3
4
use InvalidArgumentException;
5
use SessionHandlerInterface;
6
use Wandu\Http\Contracts\CookieJarInterface;
7
use Wandu\Http\Contracts\ParameterInterface;
8
use Wandu\Http\Contracts\SessionInterface;
9
use Wandu\Http\Session\Configuration;
10
11
class Session extends Parameter implements SessionInterface  
12
{
13
    /** @var string */
14
    protected $id;
15
16
    /** @var \SessionHandlerInterface */
17
    protected $handler;
18
    
19
    /** @var \Wandu\Http\Session\Configuration */
20
    protected $config;
21
22 27
    public static function createFromCookieJar(
23
        CookieJarInterface $cookieJar,
24
        SessionHandlerInterface $handler,
25
        Configuration $config = null,
26
        ParameterInterface $fallback = null
27
    ) {
28 27
        $config = $config ?: new Configuration();
29 27
        $sessionName = $config->getName();
30 27
        $id = $cookieJar->has($sessionName)
31 26
            ? $cookieJar->get($sessionName)
32 27
            : $config->getUniqueId();
33 27
        return new static($id, $handler, $config, $fallback);
34
    }
35
    
36 27
    public function __construct(
37
        $id,
38
        SessionHandlerInterface $handler,
39
        Configuration $config,
40
        ParameterInterface $fallback = null
41
    ) {
42 27
        $this->id = $id;
43 27
        $this->handler = $handler;
44 27
        $this->config = $config;
45 27
        $params = @unserialize($handler->read($this->id));
46 27
        parent::__construct(isset($params) && is_array($params) ? $params : [], $fallback);
47 27
    }
48
49
    /**
50
     * @return array
51
     */
52 6
    public function toArray()
53
    {
54 6
        $arrayToReturn = parent::toArray();
55 6
        if (isset($arrayToReturn['__flash__'])) {
56 1
            $arrayToReturn = $arrayToReturn + $arrayToReturn['__flash__'];
57 1
            unset($arrayToReturn['__flash__'], $this->params['__flash__']);
58
        }
59 6
        return $arrayToReturn;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 14
    public function get($name, $default = null, $isStrict = false)
66
    {
67 14
        $this->validNameArgument($name);
68 14
        if (isset($this->params['__flash__'][$name]) && ($isStrict || !$isStrict && $this->params['__flash__'][$name])) {
69 2
            $resultToReturn = $this->params['__flash__'][$name];
70 2
            unset($this->params['__flash__'][$name]);
71 2
            return $resultToReturn;
72
        }
73 14
        return parent::get($name, $default, $isStrict);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 5
    public function set($name, $value)
80
    {
81 5
        $this->validNameArgument($name);
82 4
        $this->params[$name] = $value;
83 4
        return $this;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 3
    public function flash($name, $value)
90
    {
91 3
        unset($this->params[$name]);
92 3
        if (!isset($this->params['__flash__'])) {
93 3
            $this->params['__flash__'] = [];
94
        }
95 3
        $this->params['__flash__'][$name] = $value;
96 3
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 10
    public function has($name)
103
    {
104 10
        $this->validNameArgument($name);
105 10
        return parent::has($name) || // or, __flash__ exists check.
106 10
        (isset($this->params['__flash__']) && array_key_exists($name, $this->params['__flash__']));
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 3
    public function remove($name)
113
    {
114 3
        $this->validNameArgument($name);
115 3
        unset($this->params[$name]);
116 3
        return $this;
117
    }
118
119
    /**
120
     * @param mixed $name
121
     */
122 22
    protected function validNameArgument($name)
123
    {
124 22
        if (!is_string($name)) {
125 1
            throw new InvalidArgumentException(sprintf('The session name must be string; "%s"', $name));
126
        }
127 22
        if ($name === '') {
128 1
            throw new InvalidArgumentException('The session name cannot be empty.');
129
        }
130 21
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 1
    public function offsetSet($offset, $value)
136
    {
137 1
        $this->set($offset, $value);
138 1
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 1
    public function offsetUnset($offset)
144
    {
145 1
        $this->remove($offset);
146 1
    }
147
148
    /**
149
     * @param \Wandu\Http\Contracts\CookieJarInterface $cookieJar
150
     */
151 1
    public function applyToCookieJar(CookieJarInterface $cookieJar)
152
    {
153 1
        $sessionName = $this->config->getName();
154
155
        // save to handler
156 1
        $this->handler->write($this->id, serialize($this->params));
157
158
        // apply to cookie-jar
159 1
        $cookieJar->set(
160
            $sessionName,
161 1
            $this->id,
162 1
            (new \DateTime())->setTimestamp(time() + $this->config->getTimeout())
163
        );
164
165
        // garbage collection
166 1
        $pick = rand(1, max(1, $this->config->getGcFrequency()));
167 1
        if ($pick === 1) {
168
            $this->handler->gc($this->config->getTimeout());
169
        }
170 1
    }
171
}
172