Completed
Branch 2.x (1d3586)
by Julián
07:39
created

Native::open()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 12
nc 4
nop 2
1
<?php
2
3
/*
4
 * sessionware (https://github.com/juliangut/sessionware).
5
 * PSR7 session management middleware.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/sessionware
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Jgut\Sessionware\Handler;
15
16
use Jgut\Sessionware\Configuration;
17
use Jgut\Sessionware\Traits\NativeSessionTrait;
18
19
/**
20
 * Native PHP session handler.
21
 */
22
class Native extends \SessionHandler implements Handler
23
{
24
    use HandlerTrait;
25
    use NativeSessionTrait;
26
27
    /**
28
     * @var bool
29
     */
30
    protected $isFileHandler;
31
32
    /**
33
     * Native session handler constructor.
34
     */
35
    public function __construct()
36
    {
37
        $this->isFileHandler = $this->getStringIniSetting('save_handler') === 'files';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @throws \RuntimeException
44
     */
45
    public function open($savePath, $sessionName)
46
    {
47
        $this->testConfiguration();
48
49
        $savePath = $this->configuration->getSavePath();
50
        $sessionName = $this->configuration->getName();
51
52
        $savePathParts = explode(DIRECTORY_SEPARATOR, rtrim($savePath, DIRECTORY_SEPARATOR));
53
        if ($sessionName !== Configuration::SESSION_NAME_DEFAULT && $sessionName !== array_pop($savePathParts)) {
54
            $savePath .= DIRECTORY_SEPARATOR . $sessionName;
55
        }
56
57
        if ($this->isFileHandler &&
58
            (!is_dir($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath))
59
        ) {
60
            // @codeCoverageIgnoreStart
61
            throw new \RuntimeException(
62
                sprintf('Failed to create session save path "%s", directory might be write protected', $savePath)
63
            );
64
            // @codeCoverageIgnoreEnd
65
        }
66
67
        return parent::open($savePath, $sessionName);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function read($sessionId)
74
    {
75
        return $this->decryptSessionData(parent::read($sessionId));
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function write($sessionId, $sessionData)
82
    {
83
        return parent::write($sessionId, $this->encryptSessionData($sessionData));
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     *
89
     * @SuppressWarnings(PMD.ShortMethodName)
90
     * @SuppressWarnings(PMD.UnusedFormalParameter)
91
     */
92
    public function gc($maxLifetime)
93
    {
94
        return parent::gc($this->configuration->getLifetime());
95
    }
96
}
97