Passed
Push — 9.0-dev ( e3b0ad...6e45f1 )
by Radu
01:42
created

Session::unset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Framework\Libraries;
3
4
use WebServCo\Framework\Settings as S;
5
6
final class Session extends \WebServCo\Framework\AbstractLibrary
7
{
8
    public function __construct($config)
9
    {
10
        parent::__construct($config);
11
    }
12
    
13
    protected function checkSession()
14
    {
15
        if (session_status() === \PHP_SESSION_NONE) {
16
            throw new \ErrorException(
17
                'Session is not started.'
18
            );
19
        }
20
    }
21
    
22
    public function start($storagePath = null)
23
    {
24
        if (session_status() === \PHP_SESSION_ACTIVE) {
25
            throw new \ErrorException(
26
                'Could not start session, already started.'
27
            );
28
        }
29
        
30
        /**
31
         * Set cache limiter.
32
         */
33
        session_cache_limiter('public, must-revalidate');
34
        
35
        /**
36
         * Set cache expire (minutes).
37
         */
38
        session_cache_expire($this->setting('expire', '36000') / 60);
39
        
40
        /**
41
         * Set garbage collector timeout (seconds).
42
         */
43
        ini_set('session.gc_maxlifetime', $this->setting('expire', '36000'));
44
        
45
        /**
46
        * Set custom session storage path.
47
        */
48
        if (!empty($storagePath)) {
49
            ini_set('session.save_path', $storagePath);
50
            session_save_path($storagePath);
51
        }
52
    
53
        /**
54
         * Make sure garbage collector visits us.
55
         */
56
        ini_set('session.gc_probability', 1);
57
58
        session_set_cookie_params(
59
            $this->setting(sprintf('cookie%slifetime', S::DIVIDER), 60 * 60 * 24 * 14),
60
            $this->setting(sprintf('cookie%spath', S::DIVIDER), '/'),
61
            $this->setting(sprintf('cookie%sdomain', S::DIVIDER), ''),
62
            $this->setting(sprintf('cookie%ssecure', S::DIVIDER), false),
63
            $this->setting(sprintf('cookie%shttponly', S::DIVIDER), true)
64
        );
65
        
66
        session_name('webservco');
67
        
68
        if (!session_start()) {
69
            throw new \ErrorException('Failed to start session');
70
        }
71
        
72
        return true;
73
    }
74
    
75
    public function set($setting, $value)
76
    {
77
        $this->checkSession();
78
        
79
        $_SESSION = \WebServCo\Framework\ArrayStorage::set(
80
            $_SESSION,
81
            $setting,
82
            $value
83
        );
84
        return true;
85
    }
86
    
87
    public function unset($setting)
88
    {
89
        $this->checkSession();
90
        
91
        $_SESSION = \WebServCo\Framework\ArrayStorage::unset(
92
            $_SESSION,
93
            $setting
94
        );
95
        return true;
96
    }
97
    
98
    public function get($setting, $defaultValue = false)
99
    {
100
        $this->checkSession();
101
        
102
        return \WebServCo\Framework\ArrayStorage::get(
103
            $_SESSION,
104
            $setting,
105
            $defaultValue
106
        );
107
    }
108
}
109