Session::write()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of dimtrovich/cart".
5
 *
6
 * (c) 2024 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Cart\Handlers;
13
14
use Dimtrovich\Cart\Contracts\StoreManager;
15
16
class Session extends BaseHandler implements StoreManager
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21
    public function init(string $cartId, array $options = []): bool
22
    {
23
        if (session_status() === PHP_SESSION_DISABLED) {
24 2
            return false;
25
        }
26
27
        if (session_status() === PHP_SESSION_NONE) {
28 2
            session_start();
29
        }
30
31 2
        return parent::init($cartId, $options);
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function has(): bool
38
    {
39 2
        return isset($_SESSION[$this->key()]);
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function read(): array
46
    {
47 2
        return $_SESSION[$this->key()] ?? [];
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function remove(): void
54
    {
55 2
        unset($_SESSION[$this->key()]);
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    protected function write(array $value): void
62
    {
63 2
        $_SESSION[$this->key()] = $value;
64
    }
65
}
66