Passed
Push — master ( 35fb8a...2dccb2 )
by Pieter van der
05:32 queued 14s
created

Tiqr_StateStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 9
eloc 36
c 3
b 0
f 1
dl 0
loc 66
ccs 32
cts 36
cp 0.8889
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getStorage() 0 52 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 20
    public static function getStorage(string $type="file", array $options=array(), LoggerInterface $logger)
51
    {
52 20
        switch ($type) {
53 20
            case "file":
54 9
                require_once("Tiqr/StateStorage/File.php");
55 9
                if (!array_key_exists('path', $options)) {
56 1
                    throw new RuntimeException('The path is missing in the StateStorage configuration');
57
                }
58 8
                $instance = new Tiqr_StateStorage_File($options['path'], $logger);
59 8
                $instance->init();
60 8
                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(
82 2
                    $options['dsn'],
83 2
                    $options['username'],
84 2
                    $options['password'],
85 2
                    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
86
                );
87
                // Set a hard-coded default for the probability the expired state is removed
88
                // 0.1 translates to a 10% chance the garbage collection is executed
89 2
                $cleanupProbability = 0.1;
90 2
                if (array_key_exists('cleanup_probability', $options) && is_numeric($options['cleanup_probability'])) {
91 2
                    $cleanupProbability = $options['cleanup_probability'];
92
                }
93
94 2
                $tablename = $options['table'];
95 2
                $instance = new Tiqr_StateStorage_Pdo($pdoInstance, $logger, $tablename, $cleanupProbability);
96 2
                $instance->init();
97 2
                return $instance;
98
99
        }
100
101 2
        throw new RuntimeException(sprintf('Unable to create a StateStorage instance of type: %s', $type));
102
    }
103
}
104