Completed
Branch 2.x (03096d)
by Julián
08:36
created

Native   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 59
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
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
     * @SuppressWarnings(PMD.ShortMethodName)
74
     * @SuppressWarnings(PMD.UnusedFormalParameter)
75
     */
76
    public function gc($maxLifetime)
77
    {
78
        return parent::gc($this->configuration->getLifetime());
79
    }
80
}
81