Passed
Pull Request — develop (#37)
by Pieter van der
03:21
created

Tiqr_StateStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 9
eloc 32
c 3
b 0
f 1
dl 0
loc 61
ccs 28
cts 32
cp 0.875
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getStorage() 0 47 9
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
use Psr\Log\LoggerInterface;
28
29
/**
30
 * Utility class to create specific StateStorage instances.
31
 * StateStorage is used to store temporary information used during 
32
 * enrollment and authentication sessions.
33
 * @author ivo
34
 *
35
 */
36
class Tiqr_StateStorage
37
{
38
    /**
39
     * Get a storage of a certain type (default: 'file')
40
     * @param String $type The type of storage to create. Supported
41
     *                     types are 'file', 'pdo' and 'memcache'.
42
     * @param array $options The options to pass to the storage
43
     *                       instance. See the documentation
44
     *                       in the StateStorage/ subdirectory for
45
     *                       options per type.
46
     * @throws RuntimeException If an unknown type is requested.
47
     * @throws RuntimeException When the options configuration array misses a required parameter
48
     *
49
     */
50 17
    public static function getStorage($type="file", $options=array(), LoggerInterface $logger)
51
    {
52 17
        switch ($type) {
53 17
            case "file":
54 6
                require_once("Tiqr/StateStorage/File.php");
55 6
                if (!array_key_exists('path', $options)) {
56 1
                    throw new RuntimeException('The path is missing in the StateStorage configuration');
57
                }
58 5
                $instance = new Tiqr_StateStorage_File($options['path'], $logger);
59 5
                $instance->init();
60 5
                return $instance;
61 11
            case "memcache":
62
                require_once("Tiqr/StateStorage/Memcache.php");
63
                $instance = new Tiqr_StateStorage_Memcache($options, $logger);
64
                $instance->init();
65
                return $instance;
66 11
            case "pdo":
67 9
                require_once("Tiqr/StateStorage/Pdo.php");
68
69 9
                $requiredOptions = ['table', 'dsn', 'username', 'password'];
70 9
                foreach ($requiredOptions as $requirement) {
71 9
                    if (!array_key_exists($requirement, $options)) {
72 7
                        throw new RuntimeException(
73 7
                            sprintf(
74 7
                                'Please configure the "%s" configuration option for the PDO state storage',
75 9
                                $requirement
76
                            )
77
                        );
78
                    }
79
                }
80
81 2
                $pdoInstance = new PDO($options['dsn'],$options['username'],$options['password']);
82
                // Set a hard-coded default for the probability the expired state is removed
83
                // 0.1 translates to a 10% chance the garbage collection is executed
84 2
                $cleanupProbability = 0.1;
85 2
                if (array_key_exists('cleanup_probability', $options) && is_numeric($options['cleanup_probability'])) {
86 2
                    $cleanupProbability = $options['cleanup_probability'];
87
                }
88
89 2
                $tablename = $options['table'];
90 2
                $instance = new Tiqr_StateStorage_Pdo($pdoInstance, $logger, $tablename, $cleanupProbability);
91 2
                $instance->init();
92 2
                return $instance;
93
94
        }
95
96 2
        throw new RuntimeException(sprintf('Unable to create a StateStorage instance of type: %s', $type));
97
    }
98
}
99