Completed
Push — master ( c525e1...415b4f )
by Gjero
01:30
created

Pdo::load()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 25
rs 8.8571
1
<?php
2
/**
3
 * Pimf
4
 *
5
 * @copyright Copyright (c)  Gjero Krsteski (http://krsteski.de)
6
 * @license   http://opensource.org/licenses/MIT MIT License
7
 */
8
9
namespace Pimf\Session\Storages;
10
11
use Pimf\Contracts\Cleanable;
12
13
/**
14
 * @package Session_Storages
15
 * @author  Gjero Krsteski <[email protected]>
16
 */
17
class Pdo extends Storage implements Cleanable
18
{
19
    /**
20
     * @var \Pimf\Database
21
     */
22
    protected $pdo;
23
24
    /**
25
     * @param \Pimf\Database $pdo
26
     */
27
    public function __construct(\Pimf\Database $pdo)
28
    {
29
        $this->pdo = $pdo;
30
    }
31
32
    /**
33
     * Load a session from storage by a given ID.
34
     * If no session is found for the ID, null will be returned.
35
     *
36
     * @param string $key
37
     *
38
     * @return array|null
39
     */
40
    public function load($key)
41
    {
42
        try {
43
            $sth = $this->pdo->prepare(
44
                'SELECT * FROM sessions WHERE id = :id'
45
            );
46
47
            $sth->bindValue(':id', $key, \PDO::PARAM_INT);
48
            $sth->execute();
49
50
            $session = $sth->fetchObject();
51
52
            if ($session instanceof \stdClass) {
53
                return array(
54
                    'id'            => $session->id,
55
                    'last_activity' => $session->last_activity,
56
                    'data'          => unserialize($session->data)
57
                );
58
            }
59
60
            return null;
61
        } catch (\PDOException $pdoe) {
62
            return null;
63
        }
64
    }
65
66
    /**
67
     * Save a given session to storage.
68
     *
69
     * @param array $session
70
     * @param array $config
71
     * @param bool  $exists
72
     */
73
    public function save($session, $config, $exists)
74
    {
75
        if ($exists) {
76
            $sth = $this->pdo->prepare(
77
                "INSERT INTO sessions (id, last_activity, data) VALUES (:id, :last_activity, :data)"
78
            );
79
        } else {
80
            $sth = $this->pdo->prepare(
81
                "UPDATE sessions SET last_activity = :last_activity, data = :data WHERE id = :id"
82
            );
83
        }
84
85
        $sth->bindValue(':id', $session['id'], \PDO::PARAM_INT);
86
        $sth->bindValue(':last_activity', $session['last_activity']);
87
        $sth->bindValue(':data', serialize($session['data']));
88
        $sth->execute();
89
    }
90
91
    /**
92
     * Delete a session from storage by a given ID.
93
     *
94
     * @param string $key
95
     */
96
    public function delete($key)
97
    {
98
        $sth = $this->pdo->prepare(
99
            "DELETE FROM sessions WHERE id = :id"
100
        );
101
102
        $sth->bindValue(':id', $key, \PDO::PARAM_INT);
103
        $sth->execute();
104
    }
105
106
    /**
107
     * Delete all expired sessions from persistent storage.
108
     *
109
     * @param int $expiration
110
     *
111
     * @return mixed|void
112
     */
113
    public function clean($expiration)
114
    {
115
        $sth = $this->pdo->prepare(
116
            "DELETE FROM sessions WHERE last_activity < :expiration"
117
        );
118
119
        $sth->bindValue(':expiration', $expiration);
120
        $sth->execute();
121
    }
122
}
123