Completed
Pull Request — master (#308)
by
unknown
20:14
created

SRFTimezones   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
C getIcalForTimezone() 0 60 11
A formatTimezoneOffset() 0 3 2
1
<?php
2
3
/**
4
 * Create the iCalendar's vTimezone component
5
 *
6
 * @author HgO
7
 *
8
 * @ingroup SemanticResultFormats
9
 */
10
class SRFTimezones {
11
	private $m_tzid;
12
	
13
	private $m_from;
14
	private $m_to;
15
16
	public function __construct( $from, $to ) {
17
		global $wgLocalTimezone;
18
		
19
		$this->m_tzid = ($wgLocalTimezone !== null) ? $wgLocalTimezone : date_default_timezone_get();
20
		
21
		$this->m_from = $from;
22
		$this->m_to = $to;
23
	}
24
	
25
	/**
26
	 * Generate all the timezone's transitions that are needed by the events.
27
	 *
28
	 * @param int $from The minimum timestamp in the list of events
0 ignored issues
show
Bug introduced by
There is no parameter named $from. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
29
	 * @param int $to The maximum timestamp in the list of events
0 ignored issues
show
Bug introduced by
There is no parameter named $to. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
30
	 */
31
	public function getIcalForTimezone() {
32
		if ( $this->m_from === null || $this->m_to === null )
33
			return false;
34
			
35
		try {
36
			$timezone = new DateTimeZone( $this->m_tzid );
37
		} catch( Exception $e ) {
38
			return false;
39
		}
40
		
41
		$transitions = $timezone->getTransitions();
42
		
43
		$min = 0;
44
		$max = 1;
45
		foreach ( $transitions as $i => $transition ) {
46
			if ( $transition['ts'] < $this->m_from ) {
47
				$min = $i;
48
				continue;
49
			}
50
51
			if ( $transition['ts'] > $this->m_to ) {
52
				$max = $i;
53
				break;
54
			}
55
		}
56
		
57
		// cf. http://www.kanzaki.com/docs/ical/vtimezone.html
58
		$result = "BEGIN:VTIMEZONE\r\n";
59
		$result .= "TZID:{$this->m_tzid}\r\n";
60
		
61
		$transition = ( $min > 0 ) ? $transitions[$min-1] : $transitions[0];
62
		$tzfrom = $transition['offset'] / 3600;
63
		
64
		foreach ( array_slice( $transitions, $min, $max - $min ) as $transition) {
65
			$dst = ( $transition['isdst'] ) ? "DAYLIGHT" : "STANDARD";
66
			$result .= "BEGIN:$dst\r\n";
67
			
68
			$start_date = date( 'Ymd\THis', $transition['ts'] );
69
			$result .= "DTSTART:$start_date\r\n";
70
			
71
			$offset = $transition['offset'] / 3600;
72
			
73
			$offset_from = $this->formatTimezoneOffset( $tzfrom );
74
			$result .= "TZOFFSETFROM:$offset_from\r\n";
75
76
			$offset_to = $this->formatTimezoneOffset( $offset );
77
			$result .= "TZOFFSETTO:$offset_to\r\n";
78
			
79
			if ( !empty( $transition['abbr'] ) )
80
				$result .= "TZNAME:{$transition['abbr']}\r\n";
81
			
82
			$result .= "END:$dst\r\n";
83
			
84
			$tzfrom = $offset;
85
		}
86
		
87
		$result .= "END:VTIMEZONE\r\n";
88
		
89
		return $result;
90
	}
91
	
92
	/**
93
	 * Format an integer offset to '+hhii', where hh are the hours, and ii the minutes
94
	 *
95
	 * @param int $offset
96
	 */
97
	private function formatTimezoneOffset( $offset ) {
98
		return sprintf('%s%02d%02d', $offset >= 0 ? '+' : '', floor($offset), ($offset - floor($offset)) * 60);
99
	}
100
}
101