Passed
Pull Request — master (#100)
by Arman
05:16 queued 02:39
created

DatabaseHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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.8.0
13
 */
14
15
namespace Quantum\Libraries\Session\Handlers;
16
17
use Quantum\Libraries\Database\DbalInterface;
18
use SessionHandlerInterface;
19
20
/**
21
 * Class DatabaseHandler
22
 * @package Quantum\Libraries\Session
23
 */
24
class DatabaseHandler implements SessionHandlerInterface
25
{
26
27
    /**
28
     * @var DbalInterface
29
     */
30
    private $sessionModel;
31
32
    /**
33
     * DatabaseHandler constructor.
34
     * @param DbalInterface $sessionModel
35
     */
36
    public function __construct(DbalInterface $sessionModel)
37
    {
38
        $this->sessionModel = $sessionModel;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function open($path, $name): bool
45
    {
46
        if ($this->sessionModel::getConnection()) {
47
            return true;
48
        }
49
50
        return false;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function close(): bool
57
    {
58
        if (!$this->sessionModel::getConnection()) {
59
            return true;
60
        }
61
62
        return false;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function read($id): string
69
    {
70
        $result = $this->sessionModel->findOneby('session_id', $id);
71
        return $result->prop('data') ?: '';
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function write($id, $data): bool
78
    {
79
        $sessionItem = $this->sessionModel->findOneBy('session_id', $id);
80
81
        if (empty($sessionItem->asArray())) {
82
            $sessionItem = $this->sessionModel->create();
83
        }
84
85
        $sessionItem->prop('session_id', $id);
86
        $sessionItem->prop('ttl', time());
87
        $sessionItem->prop('data', $data);
88
89
        return $sessionItem->save();
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95
    public function destroy($id): bool
96
    {
97
        if ($this->sessionModel->findOneBy('session_id', $id)->asArray()) {
98
            return $this->sessionModel->findOneBy('session_id', $id)->delete();
99
        }
100
101
        return true;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function gc($max_lifetime): bool
108
    {
109
        $old = time() - $max_lifetime;
110
        return $this->sessionModel->criteria('ttl', '<', $old)->deleteMany();
111
    }
112
113
}
114