Passed
Push — develop ( e3abb6...204fb9 )
by Michiel
08:17 queued 26s
created

Tiqr_StateStorage_Pdo::keyExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php 
2
3
/**
4
 * This file is part of the tiqr project.
5
 * 
6
 * The tiqr project aims to provide an open implementation for 
7
 * authentication using mobile devices. It was initiated by 
8
 * SURFnet and developed by Egeniq.
9
 *
10
 * More information: http://www.tiqr.org
11
 *
12
 * @author Patrick Honing <[email protected]>
13
 * 
14
 * @package tiqr
15
 *
16
 * @license New BSD License - See LICENSE file for details.
17
 *
18
 * @copyright (C) 2010-2012 SURFnet BV
19
 * 
20
 * 
21
 * Create SQL table (MySQL):
22
 * CREATE TABLE `tiqrstate` (`key` varchar(255) PRIMARY KEY,`expire` int,`value` text);
23
 * 
24
 */
25
26
27
class Tiqr_StateStorage_Pdo extends Tiqr_StateStorage_Abstract
28
{
29
    /**
30
     * @var PDO
31
     */
32
    protected $handle;
33
34
    /**
35
     * @var string
36
     */
37
    private $tablename;
38
39
    /**
40
     * @var int
41
     */
42
    private $cleanupProbability;
43
44
    /**
45
     * @param PDO $pdoInstance The PDO instance where all state storage operations are performed on
46
     * @param string $tablename The tablename that is used for storing and retrieving the state storage
47
     * @param int $cleanupProbability The probability the expired state storage items are removed on a 'setValue' call. Example usage: 0 = never, 0.5 = 50% chance, 1 = always
48
     */
49 4
    public function __construct(PDO $pdoInstance, string $tablename, float $cleanupProbability)
50
    {
51 4
        if ($cleanupProbability < 0 || $cleanupProbability > 1) {
52 2
            throw new RuntimeException('The probability for removing the expired state should be expressed in a floating point value between 0 and 1.');
53
        }
54 4
        $this->cleanupProbability = $cleanupProbability;
55 4
        $this->tablename = $tablename;
56 4
        $this->handle = $pdoInstance;
57 4
    }
58
59 2
    private function keyExists($key)
60
    {
61 2
        $sth = $this->handle->prepare("SELECT `key` FROM ".$this->tablename." WHERE `key` = ?");
62 2
        $sth->execute(array($key));
63 2
        return $sth->fetchColumn();
64
    }
65
66 2
    private function cleanExpired() {
67 2
        $sth = $this->handle->prepare("DELETE FROM ".$this->tablename." WHERE `expire` < ? AND NOT `expire` = 0");
68 2
        $sth->execute(array(time()));
69 2
    }
70
    
71
    /**
72
     * (non-PHPdoc)
73
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::setValue()
74
     */
75 2
    public function setValue($key, $value, $expire=0)
76
    {
77 2
        if (((float) rand() /(float) getrandmax()) < $this->cleanupProbability) {
78 2
            $this->cleanExpired();
79
        }
80 2
        if ($this->keyExists($key)) {
81 1
            $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET `value` = ?, `expire` = ? WHERE `key` = ?");
82
        } else {
83 2
            $sth = $this->handle->prepare("INSERT INTO ".$this->tablename." (`value`,`expire`,`key`) VALUES (?,?,?)");
84
        }
85
        // $expire == 0 means never expire
86 2
        if ($expire != 0) {
87 2
            $expire+=time();    // Store unix timestamp after which the expires
88
        }
89 2
        $sth->execute(array(serialize($value),$expire,$key));
90 2
    }
91
        
92
    /**
93
     * (non-PHPdoc)
94
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::unsetValue()
95
     */
96 1
    public function unsetValue($key)
97
    {
98 1
        $sth = $this->handle->prepare("DELETE FROM ".$this->tablename." WHERE `key` = ?");
99 1
        $sth->execute(array($key));
100 1
    }
101
    
102
    /**
103
     * (non-PHPdoc)
104
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::getValue()
105
     */
106 1
    public function getValue($key)
107
    {
108 1
        if ($this->keyExists($key)) {
109 1
            $sth = $this->handle->prepare("SELECT `value` FROM ".$this->tablename." WHERE `key` = ? AND (`expire` >= ? OR `expire` = 0)");
110 1
            if (false === $sth) {
111
                return NULL;
112
            }
113 1
            if (false === $sth->execute(array($key, time())) ) {
114
                return NULL;
115
            }
116 1
            $result = $sth->fetchColumn();
117 1
            return  unserialize($result);
118
        }
119 1
        return NULL;
120
    }
121
}
122