Passed
Pull Request — develop (#20)
by Pieter van der
17:16
created

Tiqr_StateStorage_Pdo   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
eloc 30
c 3
b 0
f 0
dl 0
loc 72
ccs 33
cts 35
cp 0.9429
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValue() 0 17 5
A unsetValue() 0 4 1
A setValue() 0 12 3
A cleanExpired() 0 3 1
A keyExists() 0 5 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
    private $handle = null;
30
    private $tablename;
31
        
32 1
    private function keyExists($key)
33
    {
34 1
        $sth = $this->handle->prepare("SELECT `key` FROM ".$this->tablename." WHERE `key` = ?");
35 1
        $sth->execute(array($key));
36 1
        return $sth->fetchColumn();
37
    }
38
    
39 1
    private function cleanExpired() {
40 1
        $sth = $this->handle->prepare("DELETE FROM ".$this->tablename." WHERE `expire` < ? AND NOT `expire` = 0");
41 1
        $sth->execute(array(time()));
42 1
    }
43
    
44
    /**
45
     * (non-PHPdoc)
46
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::setValue()
47
     */
48 1
    public function setValue($key, $value, $expire=0)
49
    {
50 1
        if ($this->keyExists($key)) {
51 1
            $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET `value` = ?, `expire` = ? WHERE `key` = ?");
52
        } else {
53 1
            $sth = $this->handle->prepare("INSERT INTO ".$this->tablename." (`value`,`expire`,`key`) VALUES (?,?,?)");
54
        }
55
        // $expire == 0 means never expire
56 1
        if ($expire != 0) {
57 1
            $expire+=time();    // Store unix timestamp after which the expires
58
        }
59 1
        $res = $sth->execute(array(serialize($value),$expire,$key));
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
60 1
    }
61
        
62
    /**
63
     * (non-PHPdoc)
64
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::unsetValue()
65
     */
66 1
    public function unsetValue($key)
67
    {
68 1
        $sth = $this->handle->prepare("DELETE FROM ".$this->tablename." WHERE `key` = ?");
69 1
        $sth->execute(array($key));
70 1
    }
71
    
72
    /**
73
     * (non-PHPdoc)
74
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::getValue()
75
     */
76 1
    public function getValue($key)
77
    {
78 1
        if (rand(0, 1000) < 10) {
79 1
            $this->cleanExpired();
80
        }
81 1
        if ($this->keyExists($key)) {
82 1
            $sth = $this->handle->prepare("SELECT `value` FROM ".$this->tablename." WHERE `key` = ? AND (`expire` >= ? OR `expire` = 0)");
83 1
            if (false === $sth) {
84
                return NULL;
85
            }
86 1
            if (false === $sth->execute(array($key, time())) ) {
87
                return NULL;
88
            }
89 1
            $result = $sth->fetchColumn();
90 1
            return  unserialize($result);
91
        }
92 1
        return NULL;
93
    }
94
    
95 1
    public function __construct($config=array())
96
    {
97 1
        $this->tablename = $config['table'];
98 1
        $this->handle = new PDO($config['dsn'],$config['username'],$config['password']);
99 1
    }
100
    
101
}
102