Test Failed
Pull Request — develop (#27)
by Michiel
06:54
created

Tiqr_UserSecretStorage::getSecretStorage()   C

Complexity

Conditions 13
Paths 44

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 44.5287

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 50
ccs 9
cts 21
cp 0.4286
rs 6.6166
cc 13
nc 44
nop 3
crap 44.5287

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
 * 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-2012 SURFnet BV
18
 */
19
20
use Psr\Log\LoggerInterface;
21
22
/**
23
 * Class implementing a factory to retrieve user secrets.
24
 *
25
 * @author lineke
26
 */
27
class Tiqr_UserSecretStorage
28
{
29
    /**
30
     * Get a secret storage of a certain type (default: 'file')
31
     *
32
     * @param String $type The type of storage to create. Supported
33
     *                     types are 'file', 'pdo' or 'oathservice'.
34
     * @param array $options The options to pass to the storage
35
     *                       instance. See the documentation
36
     *                       in the UserSecretStorage/ subdirectory for
37
     *                       options per type.
38
     *
39 3
     * @return Tiqr_UserSecretStorage_Interface
40
     * @throws RuntimeException If an unknown type is requested.
41 3
     * @throws RuntimeException When the options configuration array misses a required parameter
42 3
     */
43 1
    public static function getSecretStorage(string $type = "file", LoggerInterface $logger, array $options = [])
44 1
    {
45 1
        // If not provided in config, we fall back to dummy (no) encryption
46 2
        $encryptionType = isset($config['encryption']['type']) ? $config['encryption']['type'] : 'dummy';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config seems to never exist and therefore isset should always be false.
Loading history...
47
        // If the encryption configuration is not configured, we fall back to an empty encryption configuration
48
        $encryptionOptions = isset($config['encryption']) ? $config['encryption'] : [];
49
        $encryption = Tiqr_UserStorage_Encryption::getEncryption($logger, $encryptionType, $encryptionOptions);
50 2
51 2
        switch ($type) {
52 2
            case "file":
53 2
                require_once("Tiqr/UserSecretStorage/File.php");
54
                if (!array_key_exists('path', $options)) {
55
                    throw new RuntimeException('The path is missing in the UserSecretStorage configuration');
56
                }
57
                $path = $options['path'];
58
                return new Tiqr_UserSecretStorage_File($encryption, $path, $logger);
59
            case "pdo":
60
                // Input validation on the required configuration options
61
                if (!array_key_exists('dsn', $options)) {
62
                    throw new RuntimeException('The dsn is missing in the UserSecretStorage configuration');
63
                }
64
                if (!array_key_exists('username', $options)) {
65
                    throw new RuntimeException('The username is missing in the UserSecretStorage configuration');
66
                }
67 3
                if (!array_key_exists('password', $options)) {
68
                    throw new RuntimeException('The password is missing in the UserSecretStorage configuration');
69
                }
70
71
                $tableName = isset($options['table']) ? $options['table'] : 'tiqrusersecret';
72
                $dsn = $options['dsn'];
73
                $userName = $options['username'];
74
                $password = $options['password'];
75
76
                require_once("Tiqr/UserSecretStorage/Pdo.php");
77
                return new Tiqr_UserSecretStorage_Pdo($encryption, $logger, $dsn, $userName, $password, $tableName);
78
            case "oathserviceclient":
79
                require_once("Tiqr/UserSecretStorage/OathServiceClient.php");
80
                if (!array_key_exists('apiURL', $options)) {
81
                    throw new RuntimeException('The apiURL is missing in the UserSecretStorage configuration');
82
                }
83
                if (!array_key_exists('consumerKey', $options)) {
84
                    throw new RuntimeException('The consumerKey is missing in the UserSecretStorage configuration');
85
                }
86
87
                $apiClient = new Tiqr_API_Client();
88
                $apiClient->setBaseURL($options['apiURL']);
89
                $apiClient->setConsumerKey($options['consumerKey']);
90
                return new Tiqr_UserSecretStorage_OathServiceClient($apiClient, $logger);
91
        }
92
        throw new RuntimeException(sprintf('Unable to create a UserSecretStorage instance of type: %s', $type));
93
    }
94
}
95