Passed
Push — master ( 3a5b64...21d408 )
by Radu
01:14
created

Session::setStoragePath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Framework\Libraries;
3
4
use WebServCo\Framework\Settings as S;
5
use WebServCo\Framework\Exceptions\ApplicationException;
6
7
final class Session extends \WebServCo\Framework\AbstractLibrary
8
{
9
    protected function checkSession()
10
    {
11
        if (session_status() === \PHP_SESSION_NONE) {
12
            throw new ApplicationException(
13
                'Session is not started.'
14
            );
15
        }
16
    }
17
18
    protected function setStoragePath($storagePath)
19
    {
20
        if (empty($storagePath)) {
21
            return false;
22
        }
23
24
        ini_set('session.save_path', $storagePath);
25
        $actualStoragePath = session_save_path($storagePath);
26
27
        if ($actualStoragePath != $storagePath) {
28
            if ($this->setting('strict_custom_path', true)) {
29
                throw new ApplicationException(
30
                    'Unable to set custom session storage path. ' .
31
                    sprintf('Current path: %s', $actualStoragePath)
32
                );
33
            }
34
            return false;
35
        }
36
        return true;
37
    }
38
39
    public function start($storagePath = null)
40
    {
41
        if (session_status() === \PHP_SESSION_ACTIVE) {
42
            throw new ApplicationException(
43
                'Unable to start session: already started.'
44
            );
45
        }
46
47
        /**
48
         * Set cache limiter.
49
         */
50
        session_cache_limiter('public, must-revalidate');
51
52
        /**
53
         * Set cache expire (minutes).
54
         */
55
        session_cache_expire($this->setting('expire', '36000') / 60);
56
57
        /**
58
         * Set garbage collector timeout (seconds).
59
         */
60
        ini_set('session.gc_maxlifetime', $this->setting('expire', '36000'));
61
62
        /**
63
        * Set custom session storage path.
64
        */
65
        $this->setStoragePath($storagePath);
66
67
        /**
68
         * Make sure garbage collector visits us.
69
         */
70
        ini_set('session.gc_probability', 1);
71
72
        session_set_cookie_params(
73
            $this->setting(sprintf('cookie%slifetime', S::DIVIDER), 60 * 60 * 24 * 14),
74
            $this->setting(sprintf('cookie%spath', S::DIVIDER), '/'),
75
            $this->setting(sprintf('cookie%sdomain', S::DIVIDER), ''),
76
            $this->setting(sprintf('cookie%ssecure', S::DIVIDER), false),
77
            $this->setting(sprintf('cookie%shttponly', S::DIVIDER), true)
78
        );
79
80
        session_name('webservco');
81
82
        if (!session_start()) {
83
            throw new ApplicationException('Unable to start session');
84
        }
85
86
        return true;
87
    }
88
89
    public function destroy()
90
    {
91
        $_SESSION = [];
92
        setcookie(
93
            session_name(),
94
            '',
95
            time() - 3600,
96
            $this->setting(sprintf('cookie%spath', S::DIVIDER), '/'),
97
            $this->setting(sprintf('cookie%sdomain', S::DIVIDER), ''),
98
            $this->setting(sprintf('cookie%ssecure', S::DIVIDER), false),
99
            $this->setting(sprintf('cookie%shttponly', S::DIVIDER), true)
100
        );
101
        session_destroy();
102
        return true;
103
    }
104
105
    public function regenerate()
106
    {
107
        return session_regenerate_id(true);
108
    }
109
110
    public function set($setting, $value)
111
    {
112
        $this->checkSession();
113
114
        $_SESSION = \WebServCo\Framework\ArrayStorage::set(
115
            $_SESSION,
116
            $setting,
117
            $value
118
        );
119
        return true;
120
    }
121
122
    public function get($setting, $defaultValue = false)
123
    {
124
        $this->checkSession();
125
126
        return \WebServCo\Framework\ArrayStorage::get(
127
            $_SESSION,
128
            $setting,
129
            $defaultValue
130
        );
131
    }
132
133
    public function has($setting)
134
    {
135
        return (bool) $this->get($setting);
136
    }
137
138
    public function remove($setting)
139
    {
140
        $this->checkSession();
141
142
        $_SESSION = \WebServCo\Framework\ArrayStorage::remove(
143
            $_SESSION,
144
            $setting
145
        );
146
        return true;
147
    }
148
149
    public function clear($setting)
150
    {
151
        return $this->set($setting, null);
152
    }
153
}
154