Config::getRootPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Elgg;
3
4
5
/**
6
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
7
 *
8
 * @access private
9
 *
10
 * @package    Elgg.Core
11
 * @subpackage Config
12
 * @since      1.10.0
13
 */
14
class Config {
15
	/**
16
	 * Global Elgg configuration
17
	 * 
18
	 * @var \stdClass
19
	 */
20
	private $CONFIG;
21
22
	/**
23
	 * Constructor
24
	 */
25 2
	public function __construct() {
26 2
		global $CONFIG;
27 2
		$this->CONFIG = $CONFIG;
28 2
	}
29
30
	/**
31
	 * Get the URL for the current (or specified) site
32
	 *
33
	 * @param int $site_guid The GUID of the site whose URL we want to grab
34
	 * @return string
35
	 */
36 30
	function getSiteUrl($site_guid = 0) {
37 30
		if ($site_guid == 0) {
38
			
39 30
			return $this->CONFIG->wwwroot;
40
		}
41
	
42
		$site = get_entity($site_guid);
43
	
44
		if (!$site instanceof \ElggSite) {
45
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Elgg\Config::getSiteUrl of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
46
		}
47
		/* @var \ElggSite $site */
48
	
49
		return $site->url;
50
	}
51
	
52
	/**
53
	 * Get the plugin path for this installation
54
	 *
55
	 * @return string
56
	 */
57
	function getPluginsPath() {
58
		
59
		return $this->CONFIG->pluginspath;
60
	}
61
	
62
	/**
63
	 * Get the data directory path for this installation
64
	 *
65
	 * @return string
66
	 */
67
	function getDataPath() {
68
		
69
		return $this->CONFIG->dataroot;
70
	}
71
	
72
	/**
73
	 * Get the root directory path for this installation
74
	 *
75
	 * @return string
76
	 */
77
	function getRootPath() {
78
		
79
		return $this->CONFIG->path;
80
	}
81
	
82
	/**
83
	 * Get an Elgg configuration value
84
	 *
85
	 * @param string $name      Name of the configuration value
86
	 * @param int    $site_guid null for installation setting, 0 for default site
87
	 *
88
	 * @return mixed Configuration value or null if it does not exist
89
	 */
90 1
	function get($name, $site_guid = 0) {
91
		
92
	
93 1
		$name = trim($name);
94
	
95
		// do not return $CONFIG value if asking for non-current site
96 1
		if (($site_guid === 0 || $site_guid === null || $site_guid == $this->CONFIG->site_guid) && isset($this->CONFIG->$name)) {
97 1
			return $this->CONFIG->$name;
98
		}
99
	
100
		if ($site_guid === null) {
101
			// installation wide setting
102
			$value = _elgg_services()->datalist->get($name);
103
		} else {
104
			if ($site_guid == 0) {
105
				$site_guid = (int) $this->CONFIG->site_guid;
106
			}
107
	
108
			// hit DB only if we're not sure if value isn't already loaded
109
			if (!isset($this->CONFIG->site_config_loaded) || $site_guid != $this->CONFIG->site_guid) {
110
				// site specific setting
111
				$value = _elgg_services()->configTable->get($name, $site_guid);
112
			} else {
113
				$value = null;
114
			}
115
		}
116
	
117
		// @todo document why we don't cache false
118
		if ($value === false) {
119
			return null;
120
		}
121
	
122 View Code Duplication
		if ($site_guid == $this->CONFIG->site_guid || $site_guid === null) {
123
			$this->CONFIG->$name = $value;
124
		}
125
	
126
		return $value;
127
	}
128
	
129
	/**
130
	 * Set an Elgg configuration value
131
	 *
132
	 * @warning This does not persist the configuration setting. Use elgg_save_config()
133
	 *
134
	 * @param string $name  Name of the configuration value
135
	 * @param mixed  $value Value
136
	 *
137
	 * @return void
138
	 */
139
	function set($name, $value) {
140
		
141
	
142
		$name = trim($name);
143
	
144
		$this->CONFIG->$name = $value;
145
	}
146
	
147
	/**
148
	 * Save a configuration setting
149
	 *
150
	 * @param string $name      Configuration name (cannot be greater than 255 characters)
151
	 * @param mixed  $value     Configuration value. Should be string for installation setting
152
	 * @param int    $site_guid null for installation setting, 0 for default site
153
	 *
154
	 * @return bool
155
	 */
156
	function save($name, $value, $site_guid = 0) {
157
		
158
	
159
		$name = trim($name);
160
	
161
		if (strlen($name) > 255) {
162
			_elgg_services()->logger->error("The name length for configuration variables cannot be greater than 255");
163
			return false;
164
		}
165
	
166
		if ($site_guid === null) {
167
			if (is_array($value) || is_object($value)) {
168
				return false;
169
			}
170
			$result = _elgg_services()->datalist->set($name, $value);
171
		} else {
172
			if ($site_guid == 0) {
173
				$site_guid = (int) $this->CONFIG->site_guid;
174
			}
175
			$result = _elgg_services()->configTable->set($name, $value, $site_guid);
176
		}
177
	
178 View Code Duplication
		if ($site_guid === null || $site_guid == $this->CONFIG->site_guid) {
179
			_elgg_services()->config->set($name, $value);
180
		}
181
	
182
		return $result;
183
	}
184
}