Completed
Pull Request — master (#41)
by
unknown
108:23 queued 43:16
created

CampaignCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
eloc 12
c 4
b 0
f 0
dl 0
loc 33
ccs 8
cts 8
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getCampaign() 0 7 3
A getFirstCampaign() 0 5 2
A isEmpty() 0 2 1
A filter() 0 2 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Entity\BannerSelection;
6
7
/**
8
 * @license GNU GPL v2+
9
 */
10
class CampaignCollection {
11
12
	/**
13
	 * @var Campaign[]
14
	 */
15
	private array $campaigns;
16
17 2
	public function __construct( Campaign ...$campaigns ) {
18 2
		$this->campaigns = $campaigns;
19 2
	}
20
21 2
	public function getCampaign( \DateTime $dateTime ): ?Campaign {
22 2
		foreach ( $this->campaigns as $campaign ) {
23 2
			if ( $campaign->isInActiveDateRange( $dateTime ) ) {
24 1
				return $campaign;
25
			}
26
		}
27 1
		return null;
28
	}
29
30
	public function filter( callable $isValid ): CampaignCollection {
31
		return new CampaignCollection( ...array_filter( $this->campaigns, $isValid ) );
32
	}
33
34
	public function getFirstCampaign(): Campaign {
35
		if( $this->isEmpty() ) {
36
			throw new \OutOfBoundsException( 'No campaigns found.' );
37
		}
38
		return $this->campaigns[0];
39
	}
40
41
	public function isEmpty(): bool {
42
		return count( $this->campaigns ) === 0;
43
	}
44
}