Passed
Pull Request — develop (#27)
by Michiel
06:20
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 35
CRAP Score 13

Importance

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

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
     * @return Tiqr_UserSecretStorage_Interface
40
     * @throws RuntimeException If an unknown type is requested.
41
     * @throws RuntimeException When the options configuration array misses a required parameter
42
     */
43 10
    public static function getSecretStorage(string $type = "file", LoggerInterface $logger, array $options = [])
44
    {
45
        // If not provided in config, we fall back to dummy (no) encryption
46 10
        $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 10
        $encryptionOptions = isset($config['encryption']) ? $config['encryption'] : [];
49 10
        $encryption = Tiqr_UserStorage_Encryption::getEncryption($logger, $encryptionType, $encryptionOptions);
50
51 10
        switch ($type) {
52 10
            case "file":
53 2
                require_once("Tiqr/UserSecretStorage/File.php");
54 2
                if (!array_key_exists('path', $options)) {
55 1
                    throw new RuntimeException('The path is missing in the UserSecretStorage configuration');
56
                }
57 1
                $path = $options['path'];
58 1
                return new Tiqr_UserSecretStorage_File($encryption, $path, $logger);
59 8
            case "pdo":
60
                // Input validation on the required configuration options
61 4
                if (!array_key_exists('dsn', $options)) {
62 1
                    throw new RuntimeException('The dsn is missing in the UserSecretStorage configuration');
63
                }
64 3
                if (!array_key_exists('username', $options)) {
65 1
                    throw new RuntimeException('The username is missing in the UserSecretStorage configuration');
66
                }
67 2
                if (!array_key_exists('password', $options)) {
68 1
                    throw new RuntimeException('The password is missing in the UserSecretStorage configuration');
69
                }
70
71 1
                $tableName = isset($options['table']) ? $options['table'] : 'tiqrusersecret';
72 1
                $dsn = $options['dsn'];
73 1
                $userName = $options['username'];
74 1
                $password = $options['password'];
75
76 1
                require_once("Tiqr/UserSecretStorage/Pdo.php");
77 1
                return new Tiqr_UserSecretStorage_Pdo($encryption, $logger, $dsn, $userName, $password, $tableName);
78 4
            case "oathserviceclient":
79 3
                require_once("Tiqr/UserSecretStorage/OathServiceClient.php");
80 3
                if (!array_key_exists('apiURL', $options)) {
81 1
                    throw new RuntimeException('The apiURL is missing in the UserSecretStorage configuration');
82
                }
83 2
                if (!array_key_exists('consumerKey', $options)) {
84 1
                    throw new RuntimeException('The consumerKey is missing in the UserSecretStorage configuration');
85
                }
86
87 1
                $apiClient = new Tiqr_API_Client();
88 1
                $apiClient->setBaseURL($options['apiURL']);
89 1
                $apiClient->setConsumerKey($options['consumerKey']);
90 1
                return new Tiqr_UserSecretStorage_OathServiceClient($apiClient, $logger);
91
        }
92 1
        throw new RuntimeException(sprintf('Unable to create a UserSecretStorage instance of type: %s', $type));
93
    }
94
}
95