Completed
Push — master ( 2174d5...e559e0 )
by Sean
04:20
created

SS_Datetimezone   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B Format() 0 11 6
A getCurrentCachedUser() 0 10 3
1
<?php
2
/**
3
 * Class SS_Datetimezone
4
 *
5
 * Adds customisable timezones to the nice method on {@link SS_Datetime}.
6
 */
7
class SS_Datetimezone extends SS_Datetime {
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...
Coding Style introduced by
The property $_cache_members is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
8
9
	/**
10
	 * @var \Member[]
11
	 */
12
	protected static $_cache_members = [];
13
14
	/**
15
	* Returns the date in the raw SQL-format specific to a given timezone passed from the Member class, e.g. “2006-01-18 16:32:04”
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
16
	*/
17
	public function Format($format) {
18
		if($this->value){
19
			$date = new DateTime($this->value);
20
			//if the current user has set a timezone that is not the default then use that
21
			$member = $this->getCurrentCachedUser();
22
			if ($member && $member->exists() && $member->Timezone && $member->Timezone != date_default_timezone_get()) {
23
				$date->setTimezone(new DateTimeZone($member->Timezone));
24
			}
25
			return $date->Format($format);
26
		}
27
	}
28
29
	/**
30
	 * Format can be called a lot of time in tight loops, so we cache the current user
31
	 * per request
32
	 *
33
	 * @return \Member|null
34
	 */
35
	protected function getCurrentCachedUser() {
36
		$memberID = \Member::currentUserID();
37
		if(!$memberID) {
38
			return null;
39
		}
40
		if(!isset(self::$_cache_members[$memberID])) {
41
			self::$_cache_members[$memberID] = Member::get()->byId($memberID);
42
		}
43
		return self::$_cache_members[$memberID];
44
	}
45
}
46