Tiqr_StateStorage::getStorage()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 9.0509

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 32
c 3
b 0
f 1
dl 0
loc 48
ccs 32
cts 35
cp 0.9143
rs 8.0555
cc 9
nc 9
nop 3
crap 9.0509
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
use Psr\Log\LoggerInterface;
22
23
/**
24
 * Utility class to create specific StateStorage instances.
25
 * StateStorage is used to store temporary information used during 
26
 * enrollment and authentication sessions.
27
 * @author ivo
28
 *
29
 */
30
class Tiqr_StateStorage
31
{
32
    /**
33
     * Get a storage of a certain type
34
     * @param String $type The type of storage to create. Supported
35
     *                     types are 'file', 'pdo' and 'memcache'.
36
     * @param array $options The options to pass to the storage
37
     *                       instance. See the documentation
38
     *                       in the StateStorage/ subdirectory for
39
     *                       options per type.
40
     * @throws RuntimeException If an unknown type is requested.
41
     * @throws RuntimeException When the options configuration array misses a required parameter
42
     *
43
     */
44 20
    public static function getStorage(string $type, array $options, LoggerInterface $logger)
45
    {
46
        switch ($type) {
47 20
            case "file":
48 9
                if (!array_key_exists('path', $options)) {
49 1
                    throw new RuntimeException('The path is missing in the StateStorage configuration');
50
                }
51 8
                $instance = new Tiqr_StateStorage_File($options['path'], $logger);
52 8
                $instance->init();
53 8
                return $instance;
54 11
            case "memcache":
55
                $instance = new Tiqr_StateStorage_Memcache($options, $logger);
56
                $instance->init();
57
                return $instance;
58 11
            case "pdo":
59 9
                $requiredOptions = ['table', 'dsn', 'username', 'password'];
60 9
                foreach ($requiredOptions as $requirement) {
61 9
                    if (!array_key_exists($requirement, $options)) {
62 7
                        throw new RuntimeException(
63 7
                            sprintf(
64 7
                                'Please configure the "%s" configuration option for the PDO state storage',
65 7
                                $requirement
66 7
                            )
67 7
                        );
68
                    }
69
                }
70
71 2
                $pdoInstance = new PDO(
72 2
                    $options['dsn'],
73 2
                    $options['username'],
74 2
                    $options['password'],
75 2
                    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
76 2
                );
77
                // Set a hard-coded default for the probability the expired state is removed
78
                // 0.1 translates to a 10% chance the garbage collection is executed
79 2
                $cleanupProbability = 0.1;
80 2
                if (array_key_exists('cleanup_probability', $options) && is_numeric($options['cleanup_probability'])) {
81 2
                    $cleanupProbability = $options['cleanup_probability'];
82
                }
83
84 2
                $tablename = $options['table'];
85 2
                $instance = new Tiqr_StateStorage_Pdo($pdoInstance, $logger, $tablename, $cleanupProbability);
86 2
                $instance->init();
87 2
                return $instance;
88
89
        }
90
91 2
        throw new RuntimeException(sprintf('Unable to create a StateStorage instance of type: %s', $type));
92
    }
93
}
94