Passed
Push — master ( 1c7137...b5ef5e )
by Pieter van der
03:26 queued 14s
created

Tiqr_StateStorage_Memcache   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 14
eloc 33
c 4
b 1
f 0
dl 0
loc 100
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _getKeyPrefix() 0 6 2
A unsetValue() 0 5 1
B init() 0 21 7
A getValue() 0 8 2
A setValue() 0 8 2
1
<?php 
2
/**
3
 * This file is part of the tiqr project.
4
 * 
5
 * The tiqr project aims to provide an open implementation for 
6
 * authentication using mobile devices. It was initiated by 
7
 * SURFnet and developed by Egeniq.
8
 *
9
 * More information: http://www.tiqr.org
10
 *
11
 * @author Ivo Jansch <[email protected]>
12
 * 
13
 * @package tiqr
14
 *
15
 * @license New BSD License - See LICENSE file for details.
16
 *
17
 * @copyright (C) 2010-2011 SURFnet BV
18
 */
19
20
21
/**
22
 * A StateStorage implementation using memcache to store state information.
23
 * 
24
 * When passing $options to the StateStorage::getStorage method, you can use
25
 * the following settings:
26
 * - "servers": An array of servers with "host" and "port" keys. If "port" is 
27
 *              ommited the default port is used.
28
 *              This option is optional; if servers is not specified, we will
29
 *              assume a memcache on localhost on the default memcache port.
30
 * - "prefix": If the memcache server is shared with other applications, a prefix
31
 *             can be used so that there are no key collisions with other 
32
 *             applications.
33
 * 
34
 * @author ivo
35
 */
36
class Tiqr_StateStorage_Memcache extends Tiqr_StateStorage_Abstract
37
{    
38
    /**
39
     * The memcache instance.
40
     * @var Memcache
41
     */
42
    protected $_memcache = NULL;
43
44
    /**
45
     * The flavor of memcache PHP extension we are using.
46
     * @var string
47
     */
48
    private static $extension = '';
49
50
    /**
51
     * The default configuration
52
     */
53
    const DEFAULT_HOST = '127.0.0.1';
54
    const DEFAULT_PORT =  11211;
55
    
56
    /**
57
     * Get the prefix to use for all keys in memcache.
58
     * @return String the prefix
59
     */
60
    protected function _getKeyPrefix()
61
    {
62
        if (isset($this->_options["prefix"])) {
63
            return $this->_options["prefix"];
64
        }
65
        return "";
66
    }
67
    
68
    /**
69
     * Initialize the statestorage by setting up the memcache instance.
70
     * It's not necessary to call this function, the Tiqr_StateStorage factory
71
     * will take care of that.
72
     */
73
    public function init() 
74
    {
75
        parent::init();
76
77
        $class = class_exists('\Memcache') ? '\Memcache' : (class_exists('\Memcached') ? '\Memcached' : false);
78
        self::$extension = strtolower($class);
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type false; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
        self::$extension = strtolower(/** @scrutinizer ignore-type */ $class);
Loading history...
79
        
80
        $this->_memcache = new $class();
81
82
        if (!isset($this->_options["servers"])) {
83
            $this->_memcache->addServer(self::DEFAULT_HOST, self::DEFAULT_PORT);
84
        } else {
85
            foreach ($this->_options['servers'] as $server) {
86
                if (!array_key_exists('port', $server)) {
87
                    $server['port'] = self::DEFAULT_PORT;
88
                } 
89
                if (!array_key_exists('host', $server)) {
90
                    $server['host'] = self::DEFAULT_HOST;
91
                }  
92
             
93
                $this->_memcache->addServer($server['host'], $server['port']);
94
            }
95
        }          
96
    }
97
    
98
    /**
99
     * (non-PHPdoc)
100
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::setValue()
101
     */
102
    public function setValue($key, $value, $expire=0)
103
    {  
104
        $key = $this->_getKeyPrefix().$key;
105
106
        if (self::$extension === '\memcached') {
107
            $this->_memcache->set($key, $value, $expire);
108
        } else {
109
            $this->_memcache->set($key, $value, 0, $expire);
110
        }
111
    }
112
    
113
    /**
114
     * (non-PHPdoc)
115
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::unsetValue()
116
     */
117
    public function unsetValue($key)
118
    {
119
        $key = $this->_getKeyPrefix().$key;
120
        
121
        return $this->_memcache->delete($key);
122
    }
123
    
124
    /**
125
     * (non-PHPdoc)
126
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::getValue()
127
     */
128
    public function getValue($key)
129
    {
130
        $key = $this->_getKeyPrefix().$key;
131
132
        $result = $this->_memcache->get($key);
133
        if( $result === false )
134
            return null;
135
        return $result;
136
    }
137
        
138
}
139