ApcuStorage::close()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Platine Session
5
 *
6
 * Platine Session is the lightweight implementation of php native
7
 * session handler interface
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Session
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file ApcuStorage.php
34
 *
35
 *  The Cache Storage using APCu extension to manage the cache data
36
 *
37
 *  @package    Platine\Session\Storage
38
 *  @author Platine Developers Team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   https://www.platine-php.com
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Session\Storage;
49
50
use Platine\Session\Configuration;
51
use Platine\Session\Exception\SessionException;
52
use SessionHandlerInterface;
53
54
/**
55
 * @class ApcuStorage
56
 * @package Platine\Session\Storage
57
 */
58
class ApcuStorage extends AbstractStorage
59
{
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * Create new instance
64
     */
65
    public function __construct(?Configuration $config = null)
66
    {
67
        if ((!extension_loaded('apcu')) || !((bool) ini_get('apc.enabled'))) {
68
            throw new SessionException('The session for APCu driver is not available.'
69
                            . ' Check if APCu extension is loaded and enabled.');
70
        }
71
72
        parent::__construct($config);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     * @see SessionHandlerInterface
78
     */
79
    public function read(string $sid): string|false
80
    {
81
        $success = false;
82
        /** @var mixed */
83
        $data = apcu_fetch($sid, $success);
84
85
        return $success ? $data : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $success ? $data : '' could return the type null which is incompatible with the type-hinted return false|string. Consider adding an additional type-check to rule them out.
Loading history...
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     * @see SessionHandlerInterface
91
     */
92
    public function write(string $sid, string $data): bool
93
    {
94
        /** @var bool */
95
        return apcu_store($sid, $data, (int) $this->config->get('ttl'));
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     * @see SessionHandlerInterface
101
     */
102
    public function destroy(string $sid): bool
103
    {
104
        return apcu_delete($sid) === true;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     * @see SessionHandlerInterface
110
     */
111
    public function close(): bool
112
    {
113
        return true;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     * @see SessionHandlerInterface
119
     */
120
    public function gc(int $maxLifetime): bool
121
    {
122
        //APCU will do automatically
123
124
        return true;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     * @see SessionUpdateTimestampHandlerInterface
130
     */
131
    public function updateTimestamp(string $sid, string $data): bool
132
    {
133
        return $this->write($sid, $data);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     * @see SessionUpdateTimestampHandlerInterface
139
     */
140
    public function validateId(string $sid): bool
141
    {
142
        $success = false;
143
        apcu_fetch($sid, $success);
144
145
        return $success;
146
    }
147
}
148