Completed
Branch 2.x (161cec)
by Julián
03:58
created

FileHandlerTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B createSavePath() 0 17 6
1
<?php
2
3
/*
4
 * sessionware (https://github.com/juliangut/sessionware).
5
 * PSR7 compatible session management.
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\Sessionware\Traits;
13
14
use Jgut\Sessionware\Configuration;
15
16
/**
17
 * File handler helper trait.
18
 */
19
trait FileHandlerTrait
20
{
21
    /**
22
     * Create session save path.
23
     *
24
     * @param string $savePath
25
     * @param string $sessionName
26
     *
27
     * @throws \RuntimeException
28
     *
29
     * @return string
30
     */
31
    protected function createSavePath(string $savePath, string $sessionName) : string
32
    {
33
        $savePathParts = explode(DIRECTORY_SEPARATOR, rtrim($savePath, DIRECTORY_SEPARATOR));
34
        if ($sessionName !== Configuration::SESSION_NAME_DEFAULT && $sessionName !== array_pop($savePathParts)) {
35
            $savePath .= DIRECTORY_SEPARATOR . $sessionName;
36
        }
37
38
        if (!is_dir($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath)) {
39
            // @codeCoverageIgnoreStart
40
            throw new \RuntimeException(
41
                sprintf('Failed to create session save path "%s", directory might be write protected', $savePath)
42
            );
43
            // @codeCoverageIgnoreEnd
44
        }
45
46
        return $savePath;
47
    }
48
}
49