NativeSession::destroy()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 0
crap 3
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Session;
22
23
/**
24
 * Session abstraction using PHP's native session functions
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class NativeSession implements Session
30
{
31
    /**
32
     * @var array Who told you you could eat *my* cookies?!
33
     */
34
    protected $cookies = [];
35
    /**
36
     * @var Values[] Array of values, with string namespace as key
37
     */
38
    protected $values = [];
39
    /**
40
     * @var \SplObjectStorage
41
     */
42
    protected $plugins;
43
44
    /**
45
     * Creates a new native session utility.
46
     *
47
     * For example, to create a session with two plugins:
48
     *
49
     * ```php
50
     * $csrf = new \Caridea\Session\CsrfPlugin(new \Caridea\Random\Mcrypt());
51
     * $flash = new \Caridea\Session\FlashPlugin();
52
     * $session = new \Caridea\Session\NativeSession($_COOKIE, [$csrf, $flash]);
53
     * ```
54
     *
55
     * @param array $cookies A copy of the client cookies, typically from $_COOKIE
56
     * @param Plugin[] $plugins Optional array of plugins to attach to the session
57
     */
58 13
    public function __construct(array $cookies, array $plugins = [])
59
    {
60 13
        $this->cookies = $cookies;
61 13
        $this->plugins = new \SplObjectStorage();
62 13
        foreach ($plugins as $plugin) {
63 3
            if ($plugin instanceof Plugin) {
64 3
                $this->plugins->attach($plugin);
65
            }
66
        }
67 13
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72 1
    public function canResume(): bool
73
    {
74 1
        return isset($this->cookies[session_name()]);
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 1
    public function clear(): void
81
    {
82 1
        session_unset();
83 1
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88 3
    public function destroy(): bool
89
    {
90 3
        foreach (iterator_to_array($this->plugins) as $plugin) {
91
            /* @var $plugin Plugin */
92 1
            $plugin->onDestroy($this);
93
        }
94 3
        $_SESSION = [];
95 3
        if (ini_get('session.use_cookies')) {
96 2
            $params = session_get_cookie_params();
97 2
            setcookie(
98 2
                session_name(),
99 2
                '',
100 2
                time() - 42000,
101 2
                $params['path'],
102 2
                $params['domain'],
103 2
                $params['secure'],
104 2
                $params['httponly']
105
            );
106
        }
107 3
        return session_destroy();
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113 1
    public function getValues(string $namespace): Map
114
    {
115 1
        if (!isset($this->values[$namespace])) {
116 1
            $this->values[$namespace] = new Values($this, $namespace);
117
        }
118 1
        return $this->values[$namespace];
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124 1
    public function isStarted(): bool
125
    {
126 1
        return session_status() === PHP_SESSION_ACTIVE;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132 2
    public function regenerateId(): bool
133
    {
134 2
        foreach (iterator_to_array($this->plugins) as $plugin) {
135
            /* @var $plugin Plugin */
136 1
            $plugin->onRegenerate($this);
137
        }
138 2
        return session_regenerate_id(true);
139
    }
140
141
    /**
142
     * {@inheritDoc}
143
     */
144 3
    public function resume(): bool
145
    {
146 3
        if ($this->isStarted()) {
147 1
            return true;
148
        } else {
149 2
            return $this->canResume() ? $this->start() : false;
150
        }
151
    }
152
153
    /**
154
     * {@inheritDoc}
155
     */
156 2
    public function start(): bool
157
    {
158 2
        $start = session_start();
159 2
        if ($start) {
160 2
            foreach (iterator_to_array($this->plugins) as $plugin) {
161
                /* @var $plugin Plugin */
162 1
                $plugin->onStart($this);
163
            }
164
        }
165 2
        return $start;
166
    }
167
}
168