Passed
Push — master ( 86c8ba...4a8f0a )
by Tomasz
20:05 queued 07:20
created

Options   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 14
lcom 2
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 7 2
A __clone() 0 3 1
A __construct() 0 23 2
A availableOptions() 0 14 4
A optionType() 0 6 2
A assertValidOptionName() 0 9 3
1
<?php
2
3
/*
4
 * ******************************************************************************
5
 * Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 
6
 * and GN4-2 consortia
7
 *
8
 * License: see the web/copyright.php file in the file structure
9
 * ******************************************************************************
10
 */
11
12
/**
13
 * This file contains some convenience functions around option handling.
14
 *
15
 * @author Stefan Winter <[email protected]>
16
 *
17
 * @package Developer
18
 */
19
/**
20
 * necessary includes
21
 */
22
23
namespace core;
24
25
use \Exception;
26
27
/**
28
 * The Options class contains convenience functions around option handling. It is implemented as a singleton to prevent
29
 * excessive DB requests; its content never changes during a script run.
30
 *
31
 * @author Stefan Winter <[email protected]>
32
 */
33
class Options {
34
35
    /**
36
     * database which this class queries by default
37
     * 
38
     * @var string
39
     */
40
    private static $databaseType = "INST";
41
42
    /**
43
     * The (only) instance of this class
44
     * 
45
     * @var Options
46
     */
47
    private static $instance;
48
49
    /**
50
     * Our access to logging facilities
51
     * 
52
     * @var \core\common\Logging
53
     */
54
    private $loggerInstance;
55
56
    /**
57
     * This private variable contains the list of all known options and their properties (i.e. flags).
58
     * 
59
     * @var array all known options
60
     */
61
    private $typeDb;
62
63
    const TYPECODE_STRING = "string";
64
    const TYPECODE_INTEGER = "integer";
65
    const TYPECODE_TEXT = "text";
66
    const TYPECODE_BOOLEAN = "boolean";
67
    const TYPECODE_FILE = "file";
68
    const TYPECODE_COORDINATES = "coordinates";
69
70
    /**
71
     * Returns the handle to the (only) instance of this class.
72
     * 
73
     * @return Options
74
     */
75
    public static function instance() {
76
        if (!isset(self::$instance)) {
77
            $className = __CLASS__;
78
            self::$instance = new $className;
79
        }
80
        return self::$instance;
81
    }
82
83
    /**
84
     * Prevent cloning - this is a singleton.
85
     */
86
    public function __clone() {
87
        trigger_error('Cloning not allowed for singleton classes.', E_USER_ERROR);
88
    }
89
90
    /**
91
     *  Option class constructor; retrieves information about the known options from the database.
92
     */
93
    private function __construct() {
94
        $this->typeDb = [];
95
        $this->loggerInstance = new \core\common\Logging();
96
        $this->loggerInstance->debug(3, "--- BEGIN constructing Options instance ---\n");
97
        $handle = DBConnection::handle(self::$databaseType);
98
        $options = $handle->exec("SELECT name,type,flag from profile_option_dict ORDER BY name");
99
        // SELECT -> resource, not boolean
100
        while ($optionDataQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $options)) {
101
            $this->typeDb[$optionDataQuery->name] = ["type" => $optionDataQuery->type, "flag" => $optionDataQuery->flag];
102
        }
103
        $this->typeDb["general:logo_url"] = ["type" => "string", "flag" => NULL];
104
        $this->typeDb["eap:ca_url"] = ["type" => "string", "flag" => NULL];
105
        $this->typeDb["internal:country"] = ["type" => "string", "flag" => NULL];
106
        $this->typeDb["internal:profile_count"] = ["type" => "integer", "flag" => NULL];
107
        $this->typeDb["internal:checkuser_outer"] = ["type" => "boolean", "flag" => NULL];
108
        $this->typeDb["internal:checkuser_value"] = ["type" => "string", "flag" => NULL];
109
        $this->typeDb["internal:verify_userinput_suffix"] = ["type" => "boolean", "flag" => NULL];
110
        $this->typeDb["internal:hint_userinput_suffix"] = ["type" => "boolean", "flag" => NULL];
111
        $this->typeDb["internal:realm"] = ["type" => "string", "flag" => NULL];
112
        $this->typeDb["internal:use_anon_outer"] = ["type" => "boolean", "flag" => NULL];
113
        $this->typeDb["internal:anon_local_value"] = ["type" => "string", "flag" => NULL];        
114
        $this->loggerInstance->debug(3, "--- END constructing Options instance ---\n");
115
    }
116
117
    /**
118
     * This function lists all known options. If called with the optional parameter $className, only options of that class are
119
     * returned, otherwise the full set of all known attributes.
120
     * 
121
     * @assert ("user") == Array("user:email","user:fedadmin","user:realname")
122
     * 
123
     * @param string $className optionally specifies the class of options to be listed (class is the part of the option name before the : sign)
124
     * @return array of options
125
     */
126
    public function availableOptions($className = 0) {
127
        $returnArray = [];
128
        $this->loggerInstance->debug(3, "CLASSNAME IS $className\n");
129
130
        foreach (array_keys($this->typeDb) as $name) {
131
            if ($className === 0) {
132
                $returnArray[] = $name;
133
            } elseif (preg_match('/^' . $className . ':/', $name) > 0) {
134
                $returnArray[] = $name;
135
            }
136
        }
137
138
        return $returnArray;
139
    }
140
141
    /** This function returns the properties of a given attribute name. This currently means it returns its type and its flag field ("ML").
142
     *
143
     * @assert ("general:instname") == Array("type"=>"string", "flag"=>"ML")
144
     * @assert ("profile:production") == Array("type"=>"boolean", "flag"=>NULL)
145
     * 
146
     * @param string $optionname Name of the option whose properties are to be retrieved.
147
     * @return array properties of the attribute
148
     */
149
    public function optionType($optionname) {
150
        if (isset($this->typeDb[$optionname])) {
151
            return $this->typeDb[$optionname];
152
        }
153
        throw new Exception("Metadata about an option was requested, but the option name does not exist in the system: " . htmlentities($optionname));
154
    }
155
156
    /**
157
     * This function is mostly useless. It takes an (unvetted) string, sees if
158
     * it is a valid option name, and then returns the array key of the typeDb
159
     * instead of the unvetted string. This makes Scrutinizer happy.
160
     * 
161
     * @param string $unvettedName the input name
162
     * @return string the name echoed back, but from trusted source
163
     * 
164
     */
165
    public function assertValidOptionName($unvettedName) {
166
        $listOfOptions = array_keys($this->typeDb);
167
        foreach ($listOfOptions as $name) {
168
            if ($name == $unvettedName) {
169
                return $name;
170
            }
171
        }
172
        throw new Exception("Unknown option name encountered.");
173
    }
174
175
}
176