Completed
Pull Request — development (#2540)
by
unknown
02:48
created

UserSettings::_getValue()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 14
Code Lines 10

Duplication

Lines 3
Ratio 21.43 %

Importance

Changes 0
Metric Value
dl 3
loc 14
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 10
nc 6
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 4 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
	$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
	
4
	class UserSettings extends Base {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
		protected $table = 'user_settings';
6
		
7
		private $__cache = array();
8
		protected $account_id = null;
9
		private $__lazyWrite;
10
		
11
		public function __construct($account_id, $lazy_write = true){
12
			$this->account_id = $account_id;
13
			$this->__lazyWrite = $lazy_write;
14
			if (is_callable(self::$__setup_callbacks)){
15
				call_user_func(self::$__setup_callbacks, $this);			
16
			}
17
		}
18
		
19
		private static $__GetSTMT = null;
20
		private static $__SetSTMT = null;
21
		
22
		public function __destruct(){
23
			if ($this->__lazyWrite){
24
				foreach ($this->__cache as $name=>$value){
25
					$this->_storeValue($name, $value);
26
				}
27
			}
28
		}
29
		
30
		private function _storeValue($name, $value){
31 View Code Duplication
			if (empty(self::$__SetSTMT)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
				self::$__SetSTMT = $this->mysqli->prepare('REPLACE INTO '.$this->table.' (`account_id`, `name`, `value`) VALUES (?, ?, ?)');
33
			}
34
			if (!(self::$__SetSTMT && self::$__SetSTMT->bind_param('iss', $this->account_id, $name, serialize($value)) && self::$__SetSTMT->execute())) {
35
				$this->setErrorMessage($this->getErrorMsg('E0084', $this->table));
36
				return $this->sqlError();
37
			}
38
			return true;
39
		}
40
		
41
		private function _getValue($name, $default = null){
42 View Code Duplication
			if (empty(self::$__GetSTMT)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
				self::$__GetSTMT = $this->mysqli->prepare('SELECT `value` FROM '.$this->table.' WHERE `account_id` = ? AND `name` = ? LIMIT 1');
44
			}
45
			if (self::$__GetSTMT && self::$__GetSTMT->bind_param('is', $this->account_id, $name) && self::$__GetSTMT->execute() && $result = self::$__GetSTMT->get_result()) {
46
				if ($result->num_rows > 0) {
47
					return unserialize($result->fetch_object()->value);
48
				} else {
49
					return $default;
50
				}
51
			}
52
			$this->sqlError();
53
			return $default;
54
		}
55
		
56
		public function __get($name){
57
			if (!$this->__lazyWrite){
58
				return $this->_getValue($name);
59
			}
60
			if (!array_key_exists($name, $this->__cache)){
61
				$this->__cache[$name] = $this->_getValue($name);
62
			}
63
			return $this->__cache[$name];
64
		}
65
		
66
		public function __set($name, $value){
67
			if (!$this->__lazyWrite){
68
				$this->_storeValue($name, $value);
69
			} else {
70
				$this->__cache[$name] = $value;
71
			}
72
		}
73
	
74
		private static $__setup_callbacks = null;
75
		public static function setup($callback = null){
76
			self::$__setup_callbacks = $callback;
77
		}
78
		
79
		private static $__lastInstanceId;
80
		private static $__lastInstance;
81
		/**
82
		 * @param int $account_id
83
		 * @param string $lazy_write
84
		 * @return UserSettings
85
		 */
86
		public static function construct($account_id, $lazy_write = true){
87
			if ((self::$__lastInstanceId == $account_id) && (self::$__lastInstance instanceof UserSettings)){
88
				return self::$__lastInstance;
89
			}
90
			self::$__lastInstanceId = $account_id;
91
			return self::$__lastInstance = new self($account_id, $lazy_write);
0 ignored issues
show
Bug introduced by
It seems like $lazy_write defined by parameter $lazy_write on line 86 can also be of type string; however, UserSettings::__construct() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
92
		}
93
	}
94
	
95
	UserSettings::setup(function($instance)use($debug, $log, $mysqli, $aErrorCodes){
96
		$instance->setDebug($debug);
97
		$instance->setLog($log);
98
		$instance->setMysql($mysqli);
99
		$instance->setErrorCodes($aErrorCodes);
100
	});
101