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

Tiqr_StateStorage::getStorage()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 50
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 11.968

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 35
c 2
b 0
f 1
dl 0
loc 50
ccs 28
cts 35
cp 0.8
rs 7.3166
cc 11
nc 9
nop 2
crap 11.968

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Ivo Jansch <[email protected]>
13
 * 
14
 * @package tiqr
15
 *
16
 * @license New BSD License - See LICENSE file for details.
17
 *
18
 * @copyright (C) 2010-2011 SURFnet BV
19
 */
20
21
22
/**
23
 * @internal includes
24
 */
25 1
require_once("Tiqr/StateStorage/Abstract.php");
26
27
/**
28
 * Utility class to create specific StateStorage instances.
29
 * StateStorage is used to store temporary information used during 
30
 * enrollment and authentication sessions.
31
 * @author ivo
32
 *
33
 */
34
class Tiqr_StateStorage
35
{
36
    /**
37
     * Get a storage of a certain type (default: 'file')
38
     * @param String $type The type of storage to create. Supported
39
     *                     types are 'file' and 'memcache'.
40
     * @param array $options The options to pass to the storage
41
     *                       instance. See the documentation
42
     *                       in the StateStorage/ subdirectory for
43
     *                       options per type.
44
     * @throws Exception If an unknown type is requested.
45
     */
46 14
    public static function getStorage($type="file", $options=array())
47
    {
48 14
        switch ($type) {
49 14
            case "file":
50 5
                require_once("Tiqr/StateStorage/File.php");
51 5
                $instance = new Tiqr_StateStorage_File($options);
52 5
                break;
53 9
            case "memcache":
54
                require_once("Tiqr/StateStorage/Memcache.php");
55
                $instance = new Tiqr_StateStorage_Memcache($options);
56
                break;
57 9
            case "pdo":
58 8
                require_once("Tiqr/StateStorage/Pdo.php");
59
60 8
                $requiredOptions = ['table', 'dsn', 'username', 'password'];
61 8
                foreach ($requiredOptions as $requirement) {
62 8
                    if (!array_key_exists($requirement, $options)) {
63 7
                        throw new RuntimeException(
64 7
                            sprintf(
65 7
                                'Please configure the "%s" configuration option for the PDO state storage',
66 8
                                $requirement
67
                            )
68
                        );
69
                    }
70
                }
71
72 1
                $pdoInstance = new PDO($options['dsn'],$options['username'],$options['password']);
73
                // Set a hard-coded default for the probability the expired state is removed
74
                // 0.1 translates to a 10% chance the garbage collection is executed
75 1
                $cleanupProbability = 0.1;
76 1
                if (array_key_exists('cleanup_probability', $options) && is_numeric($options['cleanup_probability'])) {
77 1
                    $cleanupProbability = $options['cleanup_probability'];
78
                }
79
80 1
                $tablename = $options['table'];
81 1
                $instance = new Tiqr_StateStorage_Pdo($pdoInstance, $tablename, $cleanupProbability);
82 1
                break;
83
            default:
84 1
                if (!isset($type)) {
85
                    throw new Exception('Class name not set');
86 1
                } elseif (!class_exists($type)) {
87 1
                    throw new Exception('Class not found: ' . var_export($type, TRUE));
88
                } elseif (!is_subclass_of($type, 'Tiqr_StateStorage_Abstract')) {
89
                    throw new Exception('Class ' . $type . ' not subclass of Tiqr_StateStorage_Abstract');
90
                }
91
                $instance = new $type($options);
92
        }
93
94 6
        $instance->init();
95 6
        return $instance;
96
    }
97
}
98