Completed
Push — master ( 2c1b37...a17f23 )
by Arman
11s
created

SessionManager::getSessionHandler()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 25
rs 8.8333
cc 7
nc 11
nop 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.0.0
13
 */
14
15
namespace Quantum\Libraries\Session;
16
17
use Quantum\Exceptions\ExceptionMessages;
18
use Quantum\Libraries\Database\Database;
19
use Quantum\Libraries\Encryption\Cryptor;
20
use Quantum\Loader\Loader;
21
22
/**
23
 * Session Manager class
24
 *
25
 * @package Quantum
26
 * @category Libraries
27
 */
28
class SessionManager
29
{
30
31
    /**
32
     * @var string
33
     */
34
    private $databaseDriver = 'database';
35
36
    /**
37
     * Get session handler
38
     * @return Session
39
     * @throws \RuntimeException
40
     */
41
    public function getSessionHandler()
42
    {
43
        $driver = config()->get('session_driver');
44
45
        if (!session_id()) {
46
47
            if ($driver == $this->databaseDriver) {
48
                $orm = (new Database(new Loader()))->getORM(config()->get('session_table', 'sessions'));
49
                session_set_save_handler(new DbSessionHandler($orm), true);
50
            }
51
52
            if (@session_start() === false) {
53
                throw new \RuntimeException(ExceptionMessages::RUNTIME_SESSION_START);
54
            }
55
        }
56
57
        if (isset($_SESSION['LAST_ACTIVITY']) && time() - $_SESSION['LAST_ACTIVITY'] > config()->get('session_timeout', 1800)) {
58
            if (@session_destroy() === false) {
59
                throw new \RuntimeException(ExceptionMessages::RUNTIME_SESSION_DESTROY);
60
            }
61
        }
62
63
        $_SESSION['LAST_ACTIVITY'] = time();
64
65
        return new Session($_SESSION, new Cryptor);
66
    }
67
68
}
69