Completed
Push — master ( 33ee7a...35da7e )
by Gabriel
565:17 queued 500:16
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\Domain\Model;
6
7
use DateTimeZone;
8
9
/**
10
 * @license GPL-2.0-or-later
11
 */
12
class CampaignDate extends \DateTimeImmutable {
13
14
	private const TIMEZONE = 'UTC';
15
16
	public function __construct( string $time = 'now', ?DateTimeZone $timezone = null ) {
17
		if ( $timezone && $timezone->getName() !== self::TIMEZONE ) {
18
			throw new \InvalidArgumentException( sprintf( 'CampaignDates must have time zone "%s".', self::TIMEZONE ) );
19
		}
20
		parent::__construct( $time, new DateTimeZone( self::TIMEZONE ) );
21
	}
22
23
	public static function createFromString( string $time, ?DateTimeZone $timezone = null ): self {
24
		if ( $timezone === null ) {
25
			return new self( $time );
26
		}
27
		$instance = new \DateTime( $time, $timezone );
28
		$instance->setTimezone( new DateTimeZone( self::TIMEZONE ) );
29
		return new self( $instance->format( \DateTime::ISO8601 ) );
30
	}
31
}
32