Completed
Branch 2.x (b1c655)
by Julián
07:53
created

Native   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 58
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C open() 0 24 7
A gc() 0 4 1
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
namespace Jgut\Middleware\Sessionware\Handler;
13
14
use Jgut\Middleware\Sessionware\Configuration;
15
use Jgut\Middleware\Sessionware\SessionIniSettingsTrait;
16
17
/**
18
 * Native PHP session handler.
19
 */
20
class Native extends \SessionHandler implements Handler
21
{
22
    use HandlerTrait;
23
    use SessionIniSettingsTrait;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $isFileHandler;
29
30
    /**
31
     * Native session handler constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->isFileHandler = $this->getStringIniSetting('save_handler') === 'files';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @throws \RuntimeException
42
     */
43
    public function open($savePath, $sessionName)
44
    {
45
        $this->testConfiguration();
46
47
        $savePath = $this->configuration->getSavePath();
48
        $sessionName = $this->configuration->getName();
49
50
        $savePathParts = explode(DIRECTORY_SEPARATOR, rtrim($savePath, DIRECTORY_SEPARATOR));
51
        if ($sessionName !== Configuration::SESSION_NAME_DEFAULT && $sessionName !== array_pop($savePathParts)) {
52
            $savePath .= DIRECTORY_SEPARATOR . $sessionName;
53
        }
54
55
        if ($this->isFileHandler &&
56
            (!is_dir($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath))
57
        ) {
58
            // @codeCoverageIgnoreStart
59
            throw new \RuntimeException(
60
                sprintf('Failed to create session save path "%s", directory might be write protected', $savePath)
61
            );
62
            // @codeCoverageIgnoreEnd
63
        }
64
65
        return parent::open($savePath, $sessionName);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @SuppressWarnings(PMD.ShortMethodName)
72
     */
73
    public function gc($maxLifetime)
74
    {
75
        return parent::gc($this->configuration->getLifetime());
76
    }
77
}
78