Test Setup Failed
Push — master ( b11e68...2d8347 )
by
unknown
06:21 queued 12s
created

CampaignCollection::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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
}