Completed
Push — master ( d9f04e...3eb62a )
by Arman
21s queued 11s
created

SessionManager::getHandler()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 25
rs 8.8333
cc 7
nc 11
nop 1
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\Encryption\Cryptor;
19
use Quantum\Libraries\Database\Database;
20
use Quantum\Loader\Loader;
21
22
/**
23
 * Session Manager class
24
 * @package Quantum
25
 * @category Libraries
26
 */
27
class SessionManager
28
{
29
30
    /**
31
     * @var string
32
     */
33
    private static $databaseDriver = 'database';
34
35
    /**
36
     * GetHandler
37
     * @param Loader $loader
38
     * @return Session
39
     * @throws \RuntimeException
40
     */
41
    public static function getHandler(Loader $loader)
42
    {
43
        $driver = config()->get('session_driver');
44
45
        if (!session_id()) {
46
47
            if ($driver == self::$databaseDriver) {
48
                $orm = (new Database($loader))->getORM(config()->get('session_table', 'sessions'));
49
                session_set_save_handler(new DbSessionHandler($orm, $loader), 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