Passed
Push — master ( 38fd6d...28bc7f )
by Tim
61:09
created

CampaignDate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A createFromString() 0 7 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\BucketTesting;
6
7
use DateTimeInterface;
8
use DateTimeZone;
9
10
/**
11
 * @license GNU GPL v2+
12
 */
13
class CampaignDate extends \DateTimeImmutable {
14
15
	private const TIMEZONE = 'UTC';
16
17
	public function __construct( string $time = 'now', ?DateTimeZone $timezone = null ) {
18
		if ( $timezone && $timezone->getName() !== self::TIMEZONE ) {
19
			throw new \InvalidArgumentException( sprintf( 'CampaignDates must have time zone "%s".', self::TIMEZONE ) );
20
		}
21
		parent::__construct( $time, new DateTimeZone( self::TIMEZONE ) );
22
	}
23
24
	public static function createFromString( string $time, ?DateTimeZone $timezone = null ): self {
25
		if ( $timezone === null ) {
26
			return new self( $time );
27
		}
28
		$instance = new \DateTime( $time, $timezone );
29
		$instance->setTimezone( new DateTimeZone( self::TIMEZONE ) );
30
		return new self( $instance->format( DateTimeInterface::ISO8601 ) );
31
	}
32
}